如何提取方法的参数名称,并将它们用作Dictionary键?

时间:2014-03-24 02:29:57

标签: c# reflection refactoring c#-5.0 code-duplication

想象一下有一个简单的方法:

public async Task<ValidatePhoneNumberResult> ValidatePhoneNumberAsync(
    string phone_number,
    string country_code,
    string country_iso,
    DeviceUuid device_uuid, // DeviceUuid supports his own ToString();
    string os_name,
    string os_version,
    string model,
    string screen_resolution,
    string sim_operator = "00000",
    string is_usim = null
    )
{
    Uri uri = new Uri(MY_URI);
    HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, uri);

    Dictionary<string, string> dic = new Dictionary<string, string>();
    dic.Add("phone_number", phone_number.ToString());
    dic.Add("country_code", country_code.ToString());
    dic.Add("country_iso", country_iso.ToString());
    dic.Add("os_name", os_name.ToString());
    dic.Add("model", model.ToString());
    dic.Add("screen_resolution", screen_resolution.ToString());
    dic.Add("sim_operator", sim_operator.ToString());
    if (is_usim != null)
    {
        dic.Add("is_usim", is_usim.ToString());
    }

    request.Content = new FormUrlEncodedContent(dic);
    return await GetResult<ValidatePhoneNumberResult>(request);
}

这是我的第一个设计。从现在起我会做很多这样的功能。 但代码有一些我不喜欢的东西。它是向字典添加参数的一部分。我认为这显然是代码重复。

  • 所有参数名称都用作字典的键。
  • 他们都将实现自己的ToString()方法。
  • 如果参数为null,则不应将其放入字典中。

如果可能的话会更好:

Dictionary<string, string> dic = new Dictionary<string, string>();
dic.Add("phone_number", phone_number.ToString());
dic.Add("country_code", country_code.ToString());
dic.Add("country_iso", country_iso.ToString());
dic.Add("os_name", os_name.ToString());
dic.Add("model", model.ToString());
dic.Add("screen_resolution", screen_resolution.ToString());
dic.Add("sim_operator", sim_operator.ToString());
if (is_usim != null)
{
    dic.Add("is_usim", is_usim.ToString());
}

// To
var dic = ExtractParametersAndMakeItAsDictionary();

如何使用C#(5.0)语法创建此代码?如果你有更好的建议,我会很高兴听到。
如果不可能,是否可以用macro包裹它?(正如我们写C时经常做的那样) 告诉我任何重复删除代码的想法:)

2 个答案:

答案 0 :(得分:0)

您可以尝试这种方法:

public void SomeMethod(string p1, int p2, object p3)
{
    Dictionary<string, string> dic = ExtractParametersAndMakeItAsDictionary(p1, p2, p3);
}

private Dictionary<string, string> ExtractParametersAndMakeItAsDictionary(params object[] parameters)
{
    StackTrace stackTrace = new StackTrace();
    string methodName = stackTrace.GetFrame(1).GetMethod().Name;
    ParameterInfo[] parameterInfos = GetType().GetMethod(methodName).GetParameters();

    return parameters.Where(p => p != null).Zip(parameterInfos, (pv, pi) => new { Name = pi.Name, Value = pv.ToString() }).ToDictionary(x => x.Name, x => x.Value);
}

答案 1 :(得分:0)

为什么不使用DTO?它会使序列化变得更容易。

public static class Extensions
{
    public static Dictionary<string, string> ToDicionary(this object o)
    {
        var dic = new Dictionary<string, string>();
        foreach(var property in o.GetType().GetProperties())
        {
            dic.Add(property.Name, string.Format("{0}", property.GetValue(o)));
        }

        return dic;
    }
}

如果使用对象初始化语法,甚至不需要更改调用它的方式:

public async Task<ValidatePhoneNumberResult> ValidatePhoneNumberAsync(object dto)
{
    Uri uri = new Uri(MY_URI);
    HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, uri);

    request.Content = new FormUrlEncodedContent(dto.ToDictionary());
    return await GetResult<ValidatePhoneNumberResult>(request);
}

您甚至不需要为它创建类型:

var result = await ValidatePhoneNumberAsync(
    new
    {
            phone_number = "000000000",
            country_code = "code",
            country_iso = "iso",
            device_uuid = new DeviceUuid(),
            os_name = "Windows",
            os_version = "6.3",
            model = "model",
            screen_resolution = "4K",
            sim_operator = "00000",
            is_usim = null
    });