在构造函数中使用反射设置数据 class 的属性

set properties of data class using reflection in constructor

我有几个数据 class 以类似于下面的方式定义,我正在尝试决定是否为每个数据 class 提供一个内置构造函数来填充成员,或者在调用方法中只使用一次反射:

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

所以现在我已经走到这一步了,在每个 class 的构造函数中这样做是个好主意吗(我必须在外部调用)

或者我应该在调用方法中使用反射来设置所有属性而不必知道属性是什么...像这样(这是在循环中发生的):

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 以了解如何 return 数据 class 仅来自名称 ...

所以我最初认为我会在我的数据之外使用反射 classes 但遇到了一些障碍!

SetValueGetValue 方法接受 class 的实例作为输入,而不是 class 的 Type。当你想要set/get你所在的class属性的值时,你可以将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
    }
}

我从你的代码中不明白的是,为什么你有一个名称-值字典列表?您只需要每个对象的键值字典。我想你想启动相同 class 的多个实例,如果是这样,你必须遍历列表并通过循环启动字典的 class。像这样:

构造函数:

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