C# 将字符串映射到属性的字典
我有一个类,我想基于一个字符串返回各种属性的值,我可以将该字符串定义为如下属性:C# 将字符串映射到属性的字典,c#,dictionary,func,C#,Dictionary,Func,我有一个类,我想基于一个字符串返回各种属性的值,我可以将该字符串定义为如下属性: [FieldName("data_bus")] public string Databus { get { return _record.Databus; } } 因此,我想要一本包含以下内容的词典: private static readonly IDictionary<string, Func<string>> PropertyMap;
[FieldName("data_bus")]
public string Databus
{
get { return _record.Databus; }
}
因此,我想要一本包含以下内容的词典:
private static readonly IDictionary<string, Func<string>> PropertyMap;
私有静态只读IDictionary属性映射;
这是在这里草签的:
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.
}
}
}
静态MyClass()
{
PropertyMap=新字典();
var myType=typeof(ArisingViewModel);
foreach(myType.GetProperties(BindingFlags.Instance | BindingFlags.Public)中的var propertyInfo)
{
if(propertyInfo.GetMethod()!=null)
{
var attr=propertyInfo.GetCustomAttribute();
if(attr==null)
持续
PropertyInfo=PropertyInfo;
属性映射添加(attr.FieldName,()=>info.Name);
//不知道我在这里干什么。
}
}
}
并以某种方式按照以下方式调用:
public static object GetPropertyValue(object obj, string field)
{
Func<string> prop;
PropertyMap.TryGetValue(field, out prop);
// return
}
公共静态对象GetPropertyValue(对象对象,字符串字段)
{
Func-prop;
PropertyMap.TryGetValue(字段,输出属性);
//返回
}
有人能告诉我怎么设置吗?我不确定我是否正确理解Func的工作原理。您需要更改字典定义,以便函数接受该类的实例
private static readonly IDictionary<string, Func<ArisingViewModel,string>> PropertyMap;
私有静态只读IDictionary属性映射;
然后您需要使用静态初始值设定项
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
}
静态MyClass()
{
PropertyMap=新字典();
var myType=typeof(ArisingViewModel);
foreach(myType.GetProperties(BindingFlags.Instance | BindingFlags.Public)中的var propertyInfo)
{
if(propertyInfo.GetMethod()!=null)
{
var attr=propertyInfo.GetCustomAttribute();
if(attr==null)
持续
PropertyInfo=PropertyInfo;
PropertyMap.Add(attr.FieldName,obj=>(string)info.GetValue(obj,null));
}
}
}
公共静态对象GetPropertyValue(ArisingViewModel对象,字符串字段)
{
Func-prop;
if(PropertyMap.TryGetValue(字段,输出属性)){
返回道具(obj);
}
return null;//如果不匹配,则返回null
}
如果愿意,您还可以使您的解决方案更通用一些
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
}
}
公共静态MyClass{
私有静态只读IDictionary属性映射;
静态MyClass()
{
PropertyMap=新字典();
var myType=typeof(T);
foreach(myType.GetProperties(BindingFlags.Instance | BindingFlags.Public)中的var propertyInfo)
{
if(propertyInfo.GetMethod()!=null)
{
var attr=propertyInfo.GetCustomAttribute();
if(attr==null)
持续
PropertyInfo=PropertyInfo;
PropertyMap.Add(attr.FieldName,obj=>(string)info.GetValue(obj,null));
}
}
}
公共静态对象GetPropertyValue(T对象,字符串字段)
{
Func-prop;
if(PropertyMap.TryGetValue(字段,输出属性)){
返回道具(obj);
}
return null;//如果不匹配,则返回null
}
}
编辑--并调用您将要执行的通用版本
var value = MyClass<ArisingViewModel>.GetPropertyValue(mymodel,"data_bus");
var value=MyClass.GetPropertyValue(mymodel,“数据总线”);
您走在正确的轨道上。请参见下面的示例,该示例与您的示例类似
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());
}
Dictionary dic=newdictionary();
专用无效测试()
{
按钮btn=新按钮();
btn.Text=“BlahBlah”;
添加(“值”,()=>btn.Text);
}
私有void test2()
{
Func-outval;
dic.TryGetValue(“值”,out out-out-val);
Show(outval());
}
在示例test()中,我定义了一个新的Button类,并为其.Text属性赋值。然后我在字典里加了一个新词条。在test2()中,我检索该Func并调用它,它返回btn.Text的字符串值。谢谢,这是一个绝对完美的答案。