简体   繁体   English

在类中获取类的属性的值

[英]Getting the value of a property of a class in a class

Ok so I have been up and down the internet looking for a solution to this question. 好的,我一直在互联网上四处寻找该问题的解决方案。 I think my title is maybe no to informative so some background. 我认为我的头衔可能对信息没有帮助,所以有一定的背景知识。

I have the following classes: 我有以下课程:

public class foo { public string Name { get; set; } }
public class foo1 { public string Name { get; set; } }
public class foo2 { public string Name { get; set; } }
public class foo3 { public string Name { get; set; } }
public class foo4 { public string Name { get; set; } }
public class foo5 { public string Name { get; set; } }

public class goo 
{ 
   public string Desc { get; set; }
   public foo f { get; set; }
   public foo1 f1 { get; set; }
   public foo2 f2 { get; set; }
   public foo3 f3 { get; set; }
   public foo4 f4 { get; set; }
}

So now my question, Using Reflection, how can I get to the value of foo.Name when only having a reference to goo. 所以现在我的问题,使用反射,当仅引用goo时如何获得foo.Name的值。

The normal Reflection code is: 正常的反射代码为:

goo g = new goo();
PropertyInfo pInfo = g.GetType().GetProperty("Name");
string Name = (string)pInfo.GetValue(g, null);

So the above code is how you get a property from the goo class. 因此,以上代码是如何从goo类获取属性的。 But now how do you get the value of foo.Desc? 但是现在您如何获得foo.Desc的值?

I tried the following which doesn't work: 我尝试了以下无效的方法:

goo g = new goo();
PropertyInfo pInfo = g.GetType().GetProperty("f");
PropertyInfo pInfo2 = pInfo.PropertyType.GetProperty("Desc");
string Name = (string)pInfo2.GetValue(pInfo.PropertyType, null);

Unfortunately I get a Mismatched object error which I can understand because I am trying to use the property type and not the actual instance of the foo class. 不幸的是,我得到了一个不匹配的对象错误,我可以理解,因为我试图使用属性类型,而不是foo类的实际实例。 I also tried to fins a way to instantiate an object from the propertyinfo but if there is a way then it eludes me. 我还尝试寻找一种从propertyinfo实例化对象的方法,但是如果有一种方法,那么我就难以理解。 I could do something like this: 我可以做这样的事情:

goo g = new goo();
PropertyInfo propInfo = g.GetType().GetProperty("f");
object tmp;

propInfo.SetValue(g, Convert.ChangeType(new foo(), propInfo.PropertyType), null);
tmp = g.f;

This works but besides having to hard code the class, that is creating a new instance and therefore of now use to me. 这是可行的,但是除了必须对类进行硬编码之外,这还创建了一个新实例,因此现在对我有用。

As I say I have been up and down looking for a solution. 正如我所说,我一直在寻找解决方案。 Everything I have found is basically variants on the "get a value of a property of a class" theme but nothing about going another level deeper. 我发现的所有内容基本上都是“获取类的属性的值”主题的变体,但无非更上一层楼。

Can anyone help? 有人可以帮忙吗? Is this even possible because I would really like to stay away from hard coding. 这是否有可能,因为我真的想远离硬编码。

EDIT: I have edited the class to more accurately represent what I am working with. 编辑:我已经编辑了该类,以更准确地表示我正在使用的内容。 Based on the comments below, I am getting the names of the foo instances from a database and that is why I am using Reflection or want to use Reflection instead of hard coding 30+ switch statements. 根据下面的评论,我从数据库中获取foo实例的名称,这就是为什么我使用Reflection或想要使用Reflection而不是对30多个switch语句进行硬编码的原因。

EDIT: Also I don't know before runtime which foo classes will be populated with data. 编辑:另外,我不知道在运行时之前哪个foo类将填充数据。 Also each foo class is different. 每个foo类也不同。 Unlike my example where each foo class has a string property, in my project each class has a different design which mirrors the database. 与我的每个foo类具有字符串属性的示例不同,在我的项目中,每个类均具有不同的设计来镜像数据库。

EDIT: So Ulugbek Umirov gave the answer. 编辑:所以乌鲁别克·乌米洛夫给出了答案。 I just didn't see it immediately. 我只是没有立即看到它。 Below my implementation so as to maybe help others in the future. 在我的实现下面,以便将来可能对其他人有所帮助。

foreach (PropertyInfo pInfo in _standard.GetType().GetProperties())
{
    if (_fullDataModel.ClassDefinitions.Contains(pInfo.Name))
    {
        PropertyInfo _std_pinfo = _standard.GetType().GetProperty(pInfo.Name);
        object g = _std_pinfo.GetValue(_standard, null);

        PropertyInfo props = g.GetType().GetProperty("showMe");
        bool showMe = (bool)props.GetValue(g, null);

        if (showMe)
        {
            string tblName = _fullDataModel.ClassDefinitions[pInfo.Name].                   PropertyGroupDefinitions.Where(p => p.TransactionsTable != true).First().Token;

            //  Use tblName to build up a dataset
        }
    }
}

This does exactly what I wanted. 这正是我想要的。 Thank you. 谢谢。

Given your current code you can do the following: 根据您当前的代码,您可以执行以下操作:

goo g = new goo();
g.f = new foo { Name = "Hello" };

PropertyInfo pInfo = g.GetType().GetProperty("f");
object f = pInfo.GetValue(g);
PropertyInfo pInfo2 = f.GetType().GetProperty("Name");
string name = (string)pInfo2.GetValue(f);

You can set arbitrary property as well: 您还可以设置任意属性:

goo g = new goo();

PropertyInfo pInfo = g.GetType().GetProperty("f");
object f = Activator.CreateInstance(pInfo.PropertyType);
PropertyInfo pInfo2 = f.GetType().GetProperty("Name");
pInfo2.SetValue(f, "Hello");
pInfo.SetValue(g, f);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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