Getting Nested Object Property Value Using Reflection

前端 未结 11 2024
甜味超标
甜味超标 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: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}")
                    }
                }
            }
    

提交回复
热议问题