samedi 23 février 2019

Using reflection for fields

Is it better to use reflection in my case? I develop library for working with vk api. My code for base class that build uri for requests:

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 is a base class. Derived class must override GetMethodApiParams() method. GetRequestString() call GetMethodApiParams() to get params of derived class. For example

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));
    }
}

And another way without GetMethodApiParams() method using reflection:

    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);
    }

Derived class example:

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

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

What way is better to use?





Aucun commentaire:

Enregistrer un commentaire