通过反射递归查找对象属性?

时间:2015-10-24 19:01:38

标签: c#

我们说我上课了,

class A
{
  B PropA{get; set;}
}

class B
{
  string PropB{get; set;}
  C PropC{get; set;}
}

class C
{
  string PropD{get; set;}
}

现在我想得到" PropA.PropB"?同样,我想得到" PropA.PropC.PropD"等等?我需要创建一个方法,它将采用" PropA.PropB"作为参数并返回PropetyInfo?

4 个答案:

答案 0 :(得分:1)

static class Program
{
    static readonly BindingFlags flags = BindingFlags.Static | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic;

    static void Main() {
        var a = new A { PropA = new B() { PropB = "Value" } };
        var prop = GetPropertyRecursive("PropA.PropB", a);
    }

    static object GetPropertyRecursive(string property, object obj) {
        var splitted = property.Split('.');
        var value = obj.GetType().GetProperty(splitted[0], flags).GetValue(obj);

        if (value == null) {
            return null;
        }

        if (splitted.Length == 1) {
            return value;
        }

        return GetPropertyRecursive(string.Join(".", splitted.Skip(1)), value);
    }
}

答案 1 :(得分:1)

假设我们不知道PropAPropC的属性名称,但只知道它们的类型,并且我们也知道目标类C中属性string PropD的名称:

class A
{
    public B PropA { get; set; }
}

class B
{
    public string PropB { get; set; }
    public C PropC { get; set; }
}

class C
{
    public string PropD { get; set; }
}

class Program
{
    static object GetPValue(Type propType, object obj)
    {
        return obj.GetType().GetProperties()
            .First(p => p.PropertyType == propType).GetValue(obj);
    }

    static object GetPValue(string name, object obj)
    {
        return obj.GetType().GetProperty(name).GetValue(obj);
    }

    static void Main(string[] args)
    {
        A a = new A();
        B b = new B();
        C c = new C();
        a.PropA = b;
        b.PropB = "B";
        b.PropC = c;
        c.PropD = "C";

        object obj = new object();

        obj = GetPValue(typeof(C), GetPValue(typeof(B), a));

        Console.WriteLine(GetPValue("PropD", obj));
    }
}

输出:C

答案 2 :(得分:1)

我公开了您的属性,使下面的示例工作:

static void Main(string[] args)
{

    var propertyInfo = GetPropertyInfo(typeof(A), "PropA.PropC");
    Console.WriteLine(propertyInfo.Name);
    Console.ReadLine();
}

static PropertyInfo GetPropertyInfo(Type type, string propertyChain)
{
    if (!propertyChain.Contains("."))
    {
        var lastProperties = type.GetProperties().Where(m => m.Name.Equals(propertyChain));
        return lastProperties.Any() ? lastProperties.First() : null;
    }

    var startingName = propertyChain.Split('.')[0];
    var found = type.GetProperties().Where(m => m.Name.Equals(startingName));
    return found.Any() ? GetPropertyInfo(found.First().PropertyType, propertyChain.Replace(startingName + ".", "")) : null;
}

答案 3 :(得分:0)

我不太明白你的问题......

如果要获取类属性的名称,可以执行以下操作:

using System.Linq;
using System.Reflection;

List<Type> properties = GetTypesInNamespace(Assembly.GetExecutingAssembly(), "YourClassNameSpace");

 var propertiesNames = properties.Select(p => p.Name);

private List<Type> GetTypesInNamespace(Assembly assembly, string nameSpace)
{
    return assembly.GetTypes().Where(t => String.Equals(t.Namespace, nameSpace, StringComparison.Ordinal)).ToList();
}