简体   繁体   English

在C#中使用Reflection获取BaseType属性

[英]Get BaseType properties with Reflection in C#

I need to list all properties for containing class, I have a lot of them, so I don't know the type in the code, but I can get it trough prop.BastType.FullName, but I can't cast it, and I don't want to change all my classes to crate "Cast" methods everywere. 我需要列出包含类的所有属性,我有很多,所以我不知道代码中的类型,但是我可以通过prop.BastType.FullName来获取它,但是我不能进行强制转换,并且我不想更改所有类以创建每个“铸造”方法。 To avoid printing Property Properties such as DeclaringType,ReflectedType, etc, I'm excluding them in the code, but I don't like this way... 为了避免打印诸如DeclaringType,ReflectedType等的Property Properties,我在代码中排除了它们,但是我不喜欢这种方式...

if (("DeclaringType,ReflectedType,MetadataToken,Module,PropertyType,Attributes,CanRead" +
                    ",CanWrite,GetMethod,SetMethod,IsSpecialName,CustomAttributes,MemberType")
                    .Contains(prop.Name))
                    continue;

full code of Method: 方法的完整代码:

private static void GetAllPropertiesFor(object oo, int level, List<KeyValuePair<string,string>> result)
{
    Type entType = oo.GetType();
    IList<PropertyInfo> props = new List<PropertyInfo>(entType.GetProperties());
    foreach (PropertyInfo prop in props)
    {
        object propValue = prop.GetValue(oo, null);
        if (("DeclaringType,ReflectedType,MetadataToken,Module,PropertyType,Attributes,CanRead" +
            ",CanWrite,GetMethod,SetMethod,IsSpecialName,CustomAttributes,MemberType")
            .Contains(prop.Name))
            continue;
        if (propValue == null)
        {
            result.Add(new KeyValuePair<string, string>((new string(' ', level * 2)) + prop.Name, "Object"));
            //GetAllPropertiesFor(prop, level + (1*3), result);
        }
        else
        {
            if (result == null) result = new List<KeyValuePair<string, string>>();
            result.Add(new KeyValuePair<string, string>((new string(' ', level)) + prop.Name, propValue.ToString()));
        }
        if (entType.Namespace == "System.Data.Entity.DynamicProxies" && entType.BaseType.FullName.StartsWith("RE2012.Data.Models."))
        {
            GetAllPropertiesFor(prop, level + 1, result);
        }
        // Do something with propValue
    }
}

any suggestions? 有什么建议么?

this way I don't have in results, the values of: 这样我没有结果,值:

PropertyType : System.Int32
Attributes : None
CanRead : True
CanWrite : True

just what I need. 正是我所需要的。

Disclaim i can't really work out what you want so this answer is just a shot in the dark 免责声明 我无法真正解决您想要的东西,所以此答案只是在黑暗中拍摄

Based on what i think i understood you want to get the Properties from a specific Class so here is my example: 基于我的理解,您希望从特定的类中获取属性,因此这是我的示例:

First my same Inheritance 首先,我的继承

class BaseClass
{
    public int Base { get; set; }
}
class ChildClass : BaseClass
{
    public double Child { get; set; }
}
class ChildChildClass : ChildClass
{
    public string ChildChild { get; set; }
}

now lets find the single Properties 现在让我们查找单个属性

class Program
{

    static void Main(string[] args)
    {
        var obj = new ChildChildClass();

        var ChildChildType = obj.GetType();

        var p = new Program();

        // here we get the ChildClass properties
        var t = p.getBaseType(ChildChildType, 1);
        Console.WriteLine(t.Name);
        p.getproperties(t);

        // here we get the BaseClass properties
        t = p.getBaseType(ChildChildType, 2);
        Console.WriteLine(t.Name);
        p.getproperties(t);

        // here we get the Object properties
        t = p.getBaseType(ChildChildType, 3);
        Console.WriteLine(t.Name);
        p.getproperties(t);

        Console.Read();
    }

    internal Type getBaseType(Type t, int level)
    {
        Type temp ;
        for (int i = 0; i < level; i++)
        {
            temp = t.BaseType;
            if(temp == null)
                throw new ArgumentOutOfRangeException("you are digging to deep");
            else
                t = temp;
        }

        return t;
    }

    private void getproperties(Type t)
    {
        PropertyInfo[] properties = t.GetProperties(BindingFlags.DeclaredOnly |
                                                    BindingFlags.Public |
                                                    BindingFlags.Instance);

        foreach (PropertyInfo property in properties)
        {
            Console.WriteLine(property.Name);
        }

        Console.WriteLine("");
    }
}

if you want some information about BindingFlags here is a Link 如果你想了解的BindingFlags一些信息是一个链接

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

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