[英]set properties of data class using reflection in constructor
我有幾種數據類,其定義方式與以下類似,並且正在嘗試確定是否為每個數據類都具有內置的構造函數來填充成員,或者在調用方法中僅使用反射一次:
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
我想使用反射讓我的構造函數獲取一個字典鍵-值對的列表,並且鍵匹配屬性的名稱...
然后我可以使用類似
PropertyInfo property = properties[kvp.Key];
要么
info.setValue(typeof(Deposits), value, null);
一種方法當然是遍歷我類型中的所有屬性,並在調用setValue()
之前像下面那樣檢查property.name=kvp.key
是否:
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,以了解如何僅從名稱返回數據類...
所以我最初以為我會在數據類之外使用反射,但是遇到了一些障礙!
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));
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.