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

时间:2014-02-26 11:27:32

标签: c# reflection base

我需要列出包含类的所有属性,我有很多它们,所以我不知道代码中的类型,但是我可以通过prop.BastType.FullName得到它,但我不能强制转换它,我不想改变我的所有课程,以创建“Cast”方法。 为了避免打印属性属性,如DeclaringType,ReflectedType等,我在代码中排除它们,但我不喜欢这种方式......

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

方法的完整代码:

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

有什么建议吗?

这种方式我没有结果,值为:

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

正是我需要的。

1 个答案:

答案 0 :(得分:1)

免责声明 我无法弄清楚你想要什么,所以这个答案只是在黑暗中拍摄

基于我认为我理解你想要从特定类中获取属性,所以这是我的例子:

首先是我的继承

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

现在可以找到单个属性

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("");
    }
}

如果你想要一些关于BindingFlags的信息,请点击Link