Getting Nested Object Property Value Using Reflection

前端 未结 11 2008
甜味超标
甜味超标 2020-12-03 06:55

I have the following two classes:

public class Address
{
    public string AddressLine1 { get; set; }
    public string AddressLine2 { get; set; }
    public         


        
相关标签:
11条回答
  • 2020-12-03 07:38

    A Modified version of above to get the multilevel nested properties

    private static System.Reflection.PropertyInfo GetProperty(object t, string PropertName, out object Value)
            {
                Value = "";
                var v = t.GetType().GetProperties();
                if (t.GetType().GetProperties().Count(p => p.Name == PropertName.Split('.')[0]) == 0)
                    //throw new ArgumentNullException(string.Format("Property {0}, is not exists in object {1}", PropertName, t.ToString()));
                    return null;
                if (PropertName.Split('.').Length == 1)
                {
                    var Value1 = t.GetType().GetProperty(PropertName).GetValue(t, null);
                    Value = Value1;//.ToString();
                    return t.GetType().GetProperty(PropertName);
                }
                else
                {
                    //return GetProperty(t.GetType().GetProperty(PropertName.Split('.')[0]).GetValue(t, null), PropertName.Split('.')[1], out Value);
                    return GetProperty(t.GetType().GetProperty(PropertName.Split('.')[0]).GetValue(t, null), PropertName.Substring(PropertName.IndexOf('.') + 1, PropertName.Length - PropertName.IndexOf('.') - 1), out Value);
                }
            }
    
    0 讨论(0)
  • 2020-12-03 07:45

    Recursive method, in one line...

    object GetPropertyValue(object obj, string propertyName)
    {
        return propertyName.Contains(".") ? GetPropertyValue(obj.GetType().GetProperty(propertyName.Split(".").First()).GetValue(obj), string.Join(".", propertyName.Split(".").Skip(1))) : obj != null ? obj.GetType().GetProperty(propertyName).GetValue(obj) : null;
    }
    
    0 讨论(0)
  • 2020-12-03 07:47

    I have a problem with struct type in static class, So I must use this method GetNestedType, this is example code if you know property name, If you want to getAll you can use GetNestedTypes

    ExpandoObject in this example just use for dynamic add property and value

    private void ExtractValuesFromAppconstants(string keyName)
            {
                Type type = typeof(YourClass);
                var examination = type.GetNestedType(keyName);
                if (examination != null)
                {    
                    var innerTypes = examination.GetNestedTypes();
                    foreach (var innerType in innerTypes)
                    {
                        Console.Writeline($"{innerType.Name}")
                    }
                }
            }
    
    0 讨论(0)
  • 2020-12-03 07:50

    Get Nest properties e.g., Developer.Project.Name

    private static System.Reflection.PropertyInfo GetProperty(object t, string PropertName)
                {
                    if (t.GetType().GetProperties().Count(p => p.Name == PropertName.Split('.')[0]) == 0)
                        throw new ArgumentNullException(string.Format("Property {0}, is not exists in object {1}", PropertName, t.ToString()));
                    if (PropertName.Split('.').Length == 1)
                        return t.GetType().GetProperty(PropertName);
                    else
                        return GetProperty(t.GetType().GetProperty(PropertName.Split('.')[0]).GetValue(t, null), PropertName.Split('.')[1]);
                }
    
    0 讨论(0)
  • 2020-12-03 07:51

    This will work for level 1 and level 2 object properties e.g. Firstname and Address.AddressLine1

    public object GetPropertyValue(object obj, string propertyName)
    {
        object targetObject = obj;
        string targetPropertyName = propertyName;
    
        if (propertyName.Contains('.'))
        {
            string[] split = propertyName.Split('.');
            targetObject = obj.GetType().GetProperty(split[0]).GetValue(obj, null);
            targetPropertyName = split[1];
        }
    
        return targetObject.GetType().GetProperty(targetPropertyName).GetValue(targetObject, null);
    }
    
    0 讨论(0)
提交回复
热议问题