mercredi 19 juin 2019

Can I merge this code about IFormCollection, IQueryCollection extension functions

I wondering about this code. How can I merge to one method?

Both methods give the same result. But the parameters are different. How can I merge in this case?

I tried to merge this code. but I failed. about IFormCollection, IQueryCollection

public static class extFunc
{
    public static string SerializeObject(this IFormCollection model)
    {
        if (model.Count == 0)
            return string.Empty;

        var dic = new Dictionary<string, string>();
        foreach (var key in model.Keys)
            dic.Add(key, model[key]);

        return JsonConvert.SerializeObject(dic);
    }

    public static string SerializeObject(this IQueryCollection model)
    {
        if (model.Count == 0)
            return string.Empty;

        var dic = new Dictionary<string, string>();
        foreach (var key in model.Keys)
            dic.Add(key, model[key]);

        return JsonConvert.SerializeObject(dic);
    }

}

I will expect like this code. But this code was failed

public string SerializeObject<T>(T model) where T : ICollection<KeyValuePair<string, StringValues>>
{
    if (model.Count == 0)
        return string.Empty;

    var dic = new Dictionary<string, string>();
    foreach (var key in model.Keys)
        dic.Add(key, model[key]);

    return JsonConvert.SerializeObject(dic);
}





Aucun commentaire:

Enregistrer un commentaire