我有几个数据类以与下面类似的方式定义,我正在尝试决定是否为每个数据类都有一个内置的构造函数来填充成员,或者在调用方法中只使用一次反射:
public class reportData
{
public List<Deposits> Deposits;
}
public class Deposits
{
public Deposits(List<Dictionary<string, string>> LPQReq)
{
PropertyInfo[] properties = typeof(Deposits).GetProperties();
foreach (Dictionary<string, string> PQList in LPQReq)
{
foreach (KeyValuePair<string, string> kvp in PQList)
{
MemberInfo info = typeof(Deposits).GetField(kvp.Key) as MemberInfo ?? typeof(Deposits).GetProperty(kvp.Key) as MemberInfo;
//PropertyInfo property = properties[kvp.Key];
// info.setvalue
}
}
foreach (PropertyInfo property in properties)
{
Console.WriteLine("Name: " + property.Name + ", Value: " + property.GetValue(typeof(Deposits), null));
}
}
public string YPBRNO { get; set; }
public string YPBNA { get; set; }
public string YPTME { get; set; }
... cut for brevity
我想使用反射让我的构造函数获取一个Dictionary键值对列表,并且该键匹配属性的名称...
然后我可以使用像
这样的东西PropertyInfo property = properties[kvp.Key];
或
info.setValue(typeof(Deposits), value, null);
当然,一种方法是遍历我的类型中的所有属性,并在调用property.name=kvp.key
之前检查setValue()
是否如此:
foreach (Dictionary<string, string> PQList in LPQReq)
{
foreach (KeyValuePair<string, string> kvp in PQList)
{
MemberInfo info = typeof(Deposits).GetField(kvp.Key) as MemberInfo ?? typeof(Deposits).GetProperty(kvp.Key) as MemberInfo;
//PropertyInfo property = properties[kvp.Key];
// info.setvalue
foreach (PropertyInfo property in properties)
{
if (property.Name==kvp.Key)
{
property.SetValue(typeof(Deposits), kvp.Value, null);
}
Console.WriteLine("Name: " + property.Name + ", Value: " + property.GetValue(typeof(Deposits), null));
}
}
}
所以现在我已经做到了这一点,以这种方式做到这一点是个好主意,在每个类的构造函数中(我必须在外部调用)
或者我应该使用调用方法中的反射来设置所有属性而不必知道属性是什么......就像这样(这发生在循环中):
Type target = Type.GetType(DocumentType);
foreach (Dictionary<string, string> PQList in LPQReq)
{
foreach (KeyValuePair<string, string> kvp in PQList)
{
PropertyInfo prop = target.GetProperty(kvp.Key);
prop.SetValue (target, kvp.Value, null);
}
Console.WriteLine("Template Name = {0}", templateName);
}
修改
只是想提一下我读过SO 1044455: c-sharp-reflection-how-to-get-class-reference-from-string以了解如何从名称中返回数据类...
所以我最初认为我会使用我的数据类之外的反射,但遇到了一些障碍!
答案 0 :(得分:1)
SetValue
和GetValue
方法接受类的实例作为该类的输入Type
。如果您想设置/获取所在类的属性值,可以将this
传递给这些方法。
public Deposits(List<Dictionary<string, string>> LPQReq)
{
PropertyInfo[] properties = typeof(Deposits).GetProperties();
foreach (Dictionary<string, string> PQList in LPQReq)
{
foreach (KeyValuePair<string, string> kvp in PQList)
{
if(properties.Any(x=>x.Name == kvp.Key) &&
properties[kvp.Key].PropertyType == typeof(string))
{
properties[kvp.Key].SetValue(this, kvp.Value);
// ^ here we pass current instance
}
}
}
foreach (PropertyInfo property in properties)
{
Console.WriteLine("Name: " + property.Name + ", Value: " +
property.GetValue(this));
// ^ here we pass current instance
}
}
我对您的代码不理解的是,为什么您有一个名称字典列表?你只需要一个每个对象的键值字典。我想你想要启动同一个类的多个实例,如果是这样,你必须遍历列表并通过循环启动带有字典的类。像这样:
构造
public Deposits(Dictionary<string, string> PQList)
{
PropertyInfo[] properties = typeof(Deposits).GetProperties();
foreach (KeyValuePair<string, string> kvp in PQList)
{
if(properties.Any(x=>x.Name == kvp.Key) &&
properties[kvp.Key].PropertyType == typeof(string))
{
properties[kvp.Key].SetValue(this, kvp.Value);
}
}
foreach (PropertyInfo property in properties)
{
Console.WriteLine("Name: " + property.Name + ", Value: " +
property.GetValue(this));
}
}
其他地方:
List<Diposits> list = new List<Diposits>()
foreach (Dictionary<string, string> PQList in LPQReq)
{
list.Add(new Diposits(PQList));
}