繁体   English   中英

在类中获取类的属性的值

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

好的,我一直在互联网上四处寻找该问题的解决方案。 我认为我的头衔可能对信息没有帮助,所以有一定的背景知识。

我有以下课程:

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

所以现在我的问题,使用反射,当仅引用goo时如何获得foo.Name的值。

正常的反射代码为:

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

因此,以上代码是如何从goo类获取属性的。 但是现在您如何获得foo.Desc的值?

我尝试了以下无效的方法:

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

不幸的是,我得到了一个不匹配的对象错误,我可以理解,因为我试图使用属性类型,而不是foo类的实际实例。 我还尝试寻找一种从propertyinfo实例化对象的方法,但是如果有一种方法,那么我就难以理解。 我可以做这样的事情:

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;

这是可行的,但是除了必须对类进行硬编码之外,这还创建了一个新实例,因此现在对我有用。

正如我所说,我一直在寻找解决方案。 我发现的所有内容基本上都是“获取类的属性的值”主题的变体,但无非更上一层楼。

有人可以帮忙吗? 这是否有可能,因为我真的想远离硬编码。

编辑:我已经编辑了该类,以更准确地表示我正在使用的内容。 根据下面的评论,我从数据库中获取foo实例的名称,这就是为什么我使用Reflection或想要使用Reflection而不是对30多个switch语句进行硬编码的原因。

编辑:另外,我不知道在运行时之前哪个foo类将填充数据。 每个foo类也不同。 与我的每个foo类具有字符串属性的示例不同,在我的项目中,每个类均具有不同的设计来镜像数据库。

编辑:所以乌鲁别克·乌米洛夫给出了答案。 我只是没有立即看到它。 在我的实现下面,以便将来可能对其他人有所帮助。

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

这正是我想要的。 谢谢。

根据您当前的代码,您可以执行以下操作:

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

您还可以设置任意属性:

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