字典将字符串映射到属性

时间:2014-10-29 10:44:58

标签: c# dictionary func

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

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

所以我想要一本字典:

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.
            }
        }
    }

并以某种方式调用:

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

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

有谁能告诉我如何设置它?我不确定我是否正确理解Func的工作原理。

2 个答案:

答案 0 :(得分:3)

您需要更改字典定义,以便该函数接受类

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

然后你需要你的静态初始化器

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
  }

如果您愿意,也可以使您的解决方案更通用。

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

编辑 - 并调用您将要执行的通用版本

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

答案 1 :(得分:0)

你走在正确的轨道上。请参阅下面的示例,其中包含与您所拥有的示例相似的内容。

Dictionary<string, Func<string>> dic = new Dictionary<string, Func<string>>();
private void test()
{
    Button btn = new Button();
    btn.Text = "BlahBlah";
    dic.Add("Value", () => btn.Text);
}

private void test2()
{
    Func<string> outval;
    dic.TryGetValue("Value", out outval);
    MessageBox.Show(outval());
}

在示例test()中,我定义了一个新的Button类,并为其.Text属性赋值。然后我在我的词典&lt;&gt;中添加一个新条目。在test2()中,我检索该Func并调用它,它返回字符串值btn.Text。

相关问题