【问题标题】:How to use reflection on nested classes in order to get values如何在嵌套类上使用反射来获取值
【发布时间】:2023-02-16 18:23:46
【问题描述】:

我必须上有嵌套类的高年级

public class Preferences
{
    public FunctionClass function { get; set; } = new FunctionClass();
    public class FunctionClass
    {
        public string programfolder { get; set; } = "";
        ...
    }

    public LoggerClass logger { get; set; } = new LoggerClass();
    public class LoggerClass 
    {
        public string logFolder { get; set; } = "Log";
        ...
    }

    public OptionClass options { get; set; } = new OptionClass();
    public class OptionClass
    {
        public bool showGraphics { get; set; } = true;
        ...
    }

    public MqttSpSetupClass MqttSpSetup { get; set; } = new MqttSpSetupClass();
    public class MqttSpSetupClass
    {
        public string strAddress { get; set; } = "localhost";
        ...
    }
}

所以我希望反射在每个内部类的所有成员上循环

PropertyInfo[] props_Outer = typeof(IoAppPreferences).GetProperties();
int counter = 0;
foreach (PropertyInfo prop_Upper in props_Outer)
{
    var sName_Outer = prop_Upper.Name;
    var val_Outer = props_Outer.GetValue(counter ++);
        
    PropertyInfo[] properties_Inner;
    switch (sName_Outer.ToUpper())
    {
        case "DIMS": properties_Inner = typeof(IoAppPreferences.DimsClass).GetProperties(); break;
     ...    
    }

             
    foreach (PropertyInfo prop_Inner in properties_Inner)
    {
        var sName = prop_Inner.Name;
        //prefs.function

        var sVal = prop_Inner.GetValue(val_Outer);<------ERROR

        switch (prop_Inner.Name.ToUpper())
        {
         ...            
        }
    }

所以我在放箭头的地方出错了。原因是 val_Outer 是 FunctionClass 函数,而如果我硬编码 prefs.function 就可以了。当然,我可以为每个人设置一个开关,但我的问题是:有没有更好的方法来解决它?

我见过this solution 但不能满足我的需求

谢谢

帕特里克

【问题讨论】:

  • 什么错误?请不要告诉那是空指针...(这是...因为props_Outer.GetValue(counter ++); 没有意义并且很可能会返回null

标签: c# wpf reflection inner-classes


【解决方案1】:

您在行 var sVal = prop_Inner.GetValue(val_Outer); 处收到错误的原因是 val_Outer 是 FunctionClass 的一个实例,而不是您试图从中获取值的内部类。

访问内部类实例,需要从外部类实例中获取属性值,然后访问内部类实例的属性值。

这是您的代码的更新版本,它使用嵌套循环来迭代内部类的属性:

Preferences prefs = new Preferences();
PropertyInfo[] props_Outer = typeof(Preferences).GetProperties();

foreach (PropertyInfo prop_Outer in props_Outer)
{
    // Get the value of the outer property
    var val_Outer = prop_Outer.GetValue(prefs);
    
    // Check if the property is an inner class
    if (prop_Outer.PropertyType.IsClass && prop_Outer.PropertyType.Namespace == typeof(Preferences).Namespace)
    {
        // Iterate over the properties of the inner class
        PropertyInfo[] properties_Inner = prop_Outer.PropertyType.GetProperties();
        foreach (PropertyInfo prop_Inner in properties_Inner)
        {
            // Get the value of the inner property
            var sVal = prop_Inner.GetValue(val_Outer);
            
            // Do something with the property value
            Console.WriteLine($"Property Name: {prop_Outer.Name}.{prop_Inner.Name}, Value: {sVal}");
        }
    }
}

在这个版本的代码中,我们使用嵌套循环首先迭代外部类属性,然后对于每个外部属性,我们检查其类型是否为内部类。如果是,我们遍历内部类的属性并使用 GetValue 方法获取每个属性的值。

请注意,我们使用 prop_Outer.PropertyType.Namespace 属性来检查外部属性的类型是否与 Preferences 类位于同一命名空间中。这是为了确保我们只迭代内部类的属性,而不是同一命名空间中的任何其他类。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-29
    • 1970-01-01
    相关资源
    最近更新 更多