简体   繁体   English

C#反射-从PropertyInfo获取属性列表

[英]c# reflection - getting list of properties from a PropertyInfo

So, as the heading says, I have an object which is propertyInfo. 因此,正如标题所述,我有一个对象,它是propertyInfo。 What I want to get is that property, but I can't seem to find a way to do it. 我想要获得的是该财产,但我似乎找不到解决办法。

Firstly I had this method: 首先,我有这种方法:

public object GetPropertyInfo(object parent, String propertyName)
    {
        object propInf = null;

        PropertyInfo[] propList = parent.GetType().GetProperties();

        foreach (PropertyInfo pInf in propList)
        {
            if (propertyName == pInf.Name)
            {
                propInf = pInf;
            }
        }

        return propInf;
    }

And it works rather well, assuming the supplied 'parent' object is a regular class and not a reflected type. 并假设提供的“父”对象是常规类而不是反射类型,它可以很好地工作。

But some of the properties returned themselves contain properties that I want to access. 但是返回的某些属性本身包含我要访问的属性。 In these instances, I need to feed the PropertyInfo back into this method and get another PropertyInfo for the property. 在这些情况下,我需要将PropertyInfo反馈回此方法,并为该属性获取另一个PropertyInfo。 But if I put a PropertyInfo object into this method, it just returns me a property list of PropertyInfo (as you might imagine). 但是,如果我将PropertyInfo对象放入此方法中,它只会向我返回PropertyInfo的属性列表(您可能会想到)。

I have read up on it and it seems that what I may want is the 'GetValue' method of the PropertyInfo class. 我已经阅读了它,似乎我想要的是PropertyInfo类的“ GetValue”方法。 I'm a little unsure of it though since I can't seem to parse what it is that the method requires. 我对此不太确定,因为我似乎无法解析该方法所需的内容。

Even so, I wrote it as such: 即使这样,我还是这样写的:

public object GetPropertyInfo(object parent, String propertyName)
    {
        object propInf = null;

        object o = null;

        if (parent is PropertyInfo)
        {
            PropertyInfo p = (parent as PropertyInfo);
            o = p.GetValue(p, null);
        }
        else
            o = parent;

        PropertyInfo[] propList = o.GetType().GetProperties();

        foreach (PropertyInfo pInf in propList)
        {
            if (propertyName == pInf.Name)
            {
                propInf = pInf;
            }
        }

        return propInf;
    }

Obviously I hoped the second one would work. 显然,我希望第二个可以工作。 It passes through the 'if' statement fine, acknowledging that it is a PropertyInfo type, but then the next part provides an exception which is the following: 它很好地通过了'if'语句,确认它是PropertyInfo类型,但是接下来的部分提供了以下异常:

TargetException: Object does not match target type. TargetException:对象与目标类型不匹配。

Maybe I made a mistake with the 'GetValue' since I'm not entirely familiar with it, but if I could do it without specifying a type, that would be great. 也许我对'GetValue'犯了一个错误,因为我并不完全熟悉它,但是如果我可以在不指定类型的情况下做到这一点,那就太好了。

Assuming I understand what you are trying to do: 假设我了解您要执行的操作:

PropertyInfo represents a property of a class without being aware of the instance of the class whose property is being inspected. PropertyInfo表示class属性但不知道正在检查其属性class实例

GetValue method, however, can provide you the value of the property for a given instance . 但是, GetValue方法可以为您提供给定实例属性值。

object value = somePropertyInfo.GetValue(someInstance);
// where someInstance is of the type which has someProperty's represented property.

If you want the properties of the Type of the property you are currently inspecting you can use PropertyInfo.PropertyType.GetProperties(); 如果需要当前正在检查的属性Type属性 ,可以使用PropertyInfo.PropertyType.GetProperties(); but this will only get you the properties of the Type of the property and not the concrete (maybe derived) Type that it contains. 但这只会为您提供属性的Type的属性,而不是属性所包含的具体(可能是派生的) Type

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

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