本文介绍了c#反射-从PropertyInfo获取属性列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,正如标题所说,我有一个属性信息对象.我想得到的是那个财产,但我似乎找不到办法.

首先我有这个方法:

public object GetPropertyInfo(object parent, String propertyName){对象 propInf = null;PropertyInfo[] propList = parent.GetType().GetProperties();foreach(propList 中的PropertyInfo pInf){if (propertyName == pInf.Name){propInf = pInf;}}返回 propInf;}

假设提供的父"对象是常规类而不是反射类型,它运行得相当好.

但是返回的一些属性本身包含我想要访问的属性.在这些情况下,我需要将 PropertyInfo 反馈回此方法并为该属性获取另一个 PropertyInfo.但是,如果我将一个 PropertyInfo 对象放入这个方法中,它只会返回一个 PropertyInfo 的属性列表(如您所想).

我已经阅读了它,似乎我想要的是 PropertyInfo 类的GetValue"方法.不过我有点不确定,因为我似乎无法解析该方法需要什么.

即便如此,我还是这样写的:

public object GetPropertyInfo(object parent, String propertyName){对象 propInf = null;对象 o = 空;如果(父级是 PropertyInfo){PropertyInfo p = (父为 PropertyInfo);o = p.GetValue(p, null);}别的o = 父母;PropertyInfo[] propList = o.GetType().GetProperties();foreach(propList 中的PropertyInfo pInf){if (propertyName == pInf.Name){propInf = pInf;}}返回 propInf;}

显然我希望第二个能奏效.它通过if"语句很好,承认它是一个 PropertyInfo 类型,但接下来的部分提供了一个异常,如下所示:

TargetException: 对象与目标类型不匹配.

也许我在GetValue"上犯了一个错误,因为我并不完全熟悉它,但如果我可以在不指定类型的情况下做到这一点,那就太好了.

解决方案

假设我明白你要做什么:

PropertyInfo 表示class 不知道属性正在检查其 属性class 的实例.

GetValue然而, 方法可以为您提供给定实例属性值.

object value = somePropertyInfo.GetValue(someInstance);//其中 someInstance 是具有 someProperty 表示的属性的类型.

如果您想要属性Type属性,您可以使用PropertyInfo.PropertyType.GetProperties(); 但这只会让您获得属性的 Type 的属性,而不是它包含的具体(可能派生)Type.>

So, as the heading says, I have an object which is 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. But if I put a PropertyInfo object into this method, it just returns me a property list of PropertyInfo (as you might imagine).

I have read up on it and it seems that what I may want is the 'GetValue' method of the PropertyInfo class. 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:

TargetException: Object does not match target type.

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.

解决方案

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.

GetValue method, however, can provide you the value of the property for a given instance.

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(); but this will only get you the properties of the Type of the property and not the concrete (maybe derived) Type that it contains.

这篇关于c#反射-从PropertyInfo获取属性列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-22 05:29