Best way to get sub properties using GetProperty

后端 未结 6 681
南方客
南方客 2020-12-08 10:21
public class Address
{
    public string ZipCode {get; set;}
}

public class Customer
{
    public Address Address {get; set;}
}

how can I access e

6条回答
  •  臣服心动
    2020-12-08 10:59

    The existing answers are fine; just an alternative perspective: in many scenarios it is desirable to use System.ComponentModel rather than direct reflection, as this allows for runtime property scenarios - i.e. how a DataTable's DataView exposes the columns as properties.

    Performance wise - by default this is largely identical, but if you are doing lots of this (for example, bulk data import/export), you can actually get significant performance increases using this approach, courtesy of HyperDescriptor.

    To use System.ComponentModel, the code is similar, but subtly different:

    static void Main()
    {
        object obj = new Customer { Address = new Address { ZipCode = "abcdef" } };
    
        object address = GetValue(obj, "Address");
        object zip = GetValue(address, "ZipCode");
    
        Console.WriteLine(zip);
    }
    static object GetValue(object component, string propertyName)
    {
        return TypeDescriptor.GetProperties(component)[propertyName].GetValue(component);
    }
    

    This then gives you the same handling as though you had used data-binding to bind to "Address.ZipCode" (glossing over some details like lists etc).

    (note that you could cast zip as string etc if you know that is the expected type)

    To get the value from a deep path (including the same list handling that data-binding uses), you would use something like:

    static object ResolveValue(object component, string path) {
        foreach(string segment in path.Split('.')) {
            if (component == null) return null;
            if(component is IListSource) {
                component = ((IListSource)component).GetList();
            }
            if (component is IList) {
                component = ((IList)component)[0];
            }
            component = GetValue(component, segment);
        }
        return component;
    }
    

    The list stuff roughly mirrors the behaviour of regular data-binding (although it omits a few things like binding-contexts, currency-managers, etc)

提交回复
热议问题