繁体   English   中英

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

[英]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,以了解如何仅从名称返回数据类...

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

SetValueGetValue方法接受类的实例作为输入,而不是该类的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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM