字典将字符串映射到属性 [英] Dictionary mapping strings to properties

查看:78
本文介绍了字典将字符串映射到属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类,我想根据可以定义为属性的字符串返回各种属性的值,如下所示:

I have a class where I would like to return the value of various properties based on a string that I can define as an attribute like so:

    [FieldName("data_bus")]
    public string Databus
    {
        get { return _record.Databus; }
    }

所以我想持有一本字典:

So I would like a dictionary holding:

private static readonly IDictionary<string, Func<string>> PropertyMap;

此处初始化的地方:

static MyClass()
    {
        PropertyMap = new Dictionary<string, Func<string>>();

        var myType = typeof(ArisingViewModel);

        foreach (var propertyInfo in myType.GetProperties(BindingFlags.Instance | BindingFlags.Public))
        {
            if (propertyInfo.GetGetMethod() != null)
            {
                var attr = propertyInfo.GetCustomAttribute<FieldNameAttribute>();

                if (attr == null)
                    continue;

                PropertyInfo info = propertyInfo;
                PropertyMap.Add(attr.FieldName, () => info.Name);
                // Not sure what I'm doing here.
            }
        }
    }

然后按照某种方式调用of:

And invoked somehow along the lines of:

   public static object GetPropertyValue(object obj, string field)
    {

        Func<string> prop;
        PropertyMap.TryGetValue(field, out prop);
        // return 
    }

有人可以教我如何设置它吗?我不确定我是否正确理解Func的工作原理。

Can anyone show me how to set this up? I'm not sure I properly understand how Func works.

推荐答案

您将需要更改字典定义,以便函数可以接受该类的实例

you will need to change your dictionary definition so that the function will accept an instance of the class

private static readonly IDictionary<string, Func<ArisingViewModel,string>> PropertyMap;

然后,您需要使用静态初始值设定项

Then you need your static initializer to be

static MyClass()
{
    PropertyMap = new Dictionary<string, Func<ArisingViewModel,string>>();

    var myType = typeof(ArisingViewModel);

    foreach (var propertyInfo in myType.GetProperties(BindingFlags.Instance | BindingFlags.Public))
    {
        if (propertyInfo.GetGetMethod() != null)
        {
            var attr = propertyInfo.GetCustomAttribute<FieldNameAttribute>();

            if (attr == null)
                continue;

            PropertyInfo info = propertyInfo;
            PropertyMap.Add(attr.FieldName, obj => (string)info.GetValue(obj,null));
        }
    }
}

public static object GetPropertyValue(ArisingViewModel obj, string field)
{
    Func<ArisingViewModel,string> prop;
    if (PropertyMap.TryGetValue(field, out prop)) {
       return prop(obj);
    }
    return null; //Return null if no match
  }

您还可以使解决方案更多

You can also make your solution a little more generic if you wish.

public static MyClass<T> {

  private static readonly IDictionary<string, Func<T,string>> PropertyMap;


  static MyClass()
  {
    PropertyMap = new Dictionary<string, Func<T,string>>();

    var myType = typeof(T);

    foreach (var propertyInfo in myType.GetProperties(BindingFlags.Instance | BindingFlags.Public))
    {
        if (propertyInfo.GetGetMethod() != null)
        {
            var attr = propertyInfo.GetCustomAttribute<FieldNameAttribute>();

            if (attr == null)
                continue;

            PropertyInfo info = propertyInfo;
            PropertyMap.Add(attr.FieldName, obj => (string)info.GetValue(obj,null));
        }
    }
  }

  public static object GetPropertyValue(T obj, string field)
  {
    Func<ArisingViewModel,string> prop;
    if (PropertyMap.TryGetValue(field, out prop)) {
       return prop(obj);
    }
    return null; //Return null if no match
  }
}

EDIT-和调用您将要使用的通用版本

EDIT -- and to call the generic version you would do

var value = MyClass<ArisingViewModel>.GetPropertyValue(mymodel,"data_bus");

这篇关于字典将字符串映射到属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆