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

标签 c# reflection properties member

我有几个数据类以与下面类似的方式定义,并试图决定是否为每个数据类都有一个内置构造函数来填充成员,或者在调用方法中仅使用一次反射:

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

关于c# - 在构造函数中使用反射设置数据类的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32124296/

相关文章:

java - 获取对象类并实例化新实例

reflection - clojure.lang.Reflector 在互操作方法的实现中是如何使用的

ios - 从另一个类访问 IBOutlet 属性

c# - 数组索引返回 null 而不是越界

c# - 在列表中创建自定义 linq 列名称

c# - 创建一个抽象函数,它根据继承者接受不同的参数

java - 获取字段名称并检查数组列表是否存在?

UML 序列图调用属性

c# - 将所有绘制的对象保存到位图中

c# - PostgreSQL:JSON 中的计算字段