对字段使用反射

时间:2019-02-23 14:53:11

标签: c# .net reflection

在我的情况下使用反射更好吗?我开发了使用vk api的库。我的基类代码为请求建立URI:

public abstract class VkApiMethod : IVkApiMethod
{
    private string _apiUri = "https://api.vk.com/method/",
                   _apiVersion = "5.92";

    public VkApiMethod(string AccessToken)
    {
        this.AccessToken = AccessToken;
        Fields = new string[] { };
    } 

    public string VkApiMethodName { get; protected set; }

    public string AccessToken { get; set; }

    public string[] Fields { get; set; }

    protected abstract string GetMethodApiParams();

    public string GetRequestString()
    {
        return string.Format("{0}{1}?access_token={2}&fields={3}{4}&v={5}", _apiUri,
                                                                         VkApiMethodName,
                                                                         AccessToken,
                                                                         ArrayToString(Fields),
                                                                         GetMethodApiParams(),
                                                                         _apiVersion);
    } 
}

VkApiMethod是基类。派生类必须重写GetMethodApiParams()方法。 GetRequestString()调用GetMethodApiParams()以获取派生类的params
例如

class GetUsers : VkApiMethod
{
    public GetUsers(string AccessToken)
        : base(AccessToken)
    {
        VkApiMethodName = "users.get";
    } 
    /// <summary>
    /// Идентификаторы пользователей или их короткие имена.
    /// </summary>
    public string[] UserIDs { get; set; }

    protected override string GetMethodApiParams()
    {
        return string.Format("&user_ids={0}", ArrayToString(UserIDs));
    }
}

另一种没有使用反射的GetMethodApiParams()方法的方法:

public string GetRequestString()
{
    var @params = from p in this.GetType().GetProperties()
                  let attr = p.GetCustomAttributes(typeof(RequestParamAttr), true)
                  where attr.Length == 1
                  select new
                  {
                      PropValue = p.GetValue(this),
                      AttrName = (attr.First() as RequestParamAttr).ParamName
                  };

    var _reqUriParams = "";
    foreach (var param in @params)
        _reqUriParams += string.Format("&{0}={1}", param.AttrName, param.PropValue);

    return string.Format("{0}{1}?access_token={2}{3}&v={4}", _apiUri,
                                                             VkApiMethodName,
                                                             AccessToken,
                                                             _reqUriParams,
                                                             _apiVersion);
}

派生类示例:

class GetUsers : VkApiMethod
{
    public GetUsers(string AccessToken)
        : base(AccessToken)
    {
        VkApiMethodName = "users.get";
    }

    /// <summary>
    /// Идентификаторы пользователей или их короткие имена.
    /// </summary>
    [RequestParamAttr("user_ids")]
    public string[] UserIDs { get; set; }
}  

哪种方法更好?

0 个答案:

没有答案