如何遍历从appSettings.json配置的Devices类属性
我有一个appSettings.json文件,其中包含如下Devices配置组:
"Devices": { "_850347_74Crompton1": "3605", "_850532_41Crompton2": "813", "_850722_18IMEElectricity": "707", "_850766_85DustNoise1": "306", "_850772_63Dustnoise2": "2866", "_850774_29DustNoise3": "3104", "_863859_63Level": "22601", "_864233_30": "713", "_864319_07noise": "606" }
对应的Devices类定义如下:
public class Devices { public string _850347_74Crompton1 { get; set; } public string _850532_41Crompton2 { get; set; } public string _850722_18IMEElectricity { get; set; } public string _850766_85DustNoise1 { get; set; } public string _850772_63Dustnoise2 { get; set; } public string _850774_29DustNoise3 { get; set; } public string _863859_63Level { get; set; } public string _864233_30 { get; set; } public string _864319_07noise { get; set; } }
我可以通过以下方式获取该对象的值:
var myDevice = config.GetRequiredSection("Devices").Get<Devices>();
也能直接访问指定属性值,例如:
var myValue = myDevice._850347_74Crompton1; var anotherValue = myDevice._864233_30;
但我不想硬编码属性名,希望遍历所有属性,尝试的代码无法正常工作,要么返回null要么类型不正确。更新后尝试如下代码出现错误:
Devices iot = config.GetRequiredSection("Devices").Get<Devices>(); foreach (Devices device in iot) { ... }
错误信息:
Severity Code Description Project File Line Suppression State
Error CS1579 foreach statement cannot operate on variables of type
'appSettings.Devices' because 'appSettings.Devices' does not contain a
public instance or extension definition for
'GetEnumerator' IoT_CaptisDataCapture .....\Program.cs 60 Active
请问如何实现遍历Devices类的属性?
方法1:使用反射遍历类的属性
通过反射获取Devices对象的所有公共属性,遍历读取属性名和值:
Devices iot = config.GetRequiredSection("Devices").Get<Devices>(); // 获取所有公共实例属性 var properties = typeof(Devices).GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance); foreach (var prop in properties) { string propName = prop.Name; string propValue = prop.GetValue(iot) as string; // 按需处理属性名和值,例如打印 Console.WriteLine($"{propName}: {propValue}"); }
方法2:直接从配置节获取键值对
无需反序列化为Devices对象,直接读取配置节的子项:
var devicesSection = config.GetRequiredSection("Devices"); foreach (var child in devicesSection.GetChildren()) { string deviceKey = child.Key; string deviceValue = child.Value; Console.WriteLine($"{deviceKey}: {deviceValue}"); }
方法3:修改Devices类实现可枚举接口
如果希望直接用foreach遍历Devices对象,让类实现IEnumerable<KeyValuePair<string, string>>,手动返回属性的键值对:
public class Devices : IEnumerable<KeyValuePair<string, string>> { public string _850347_74Crompton1 { get; set; } public string _850532_41Crompton2 { get; set; } public string _850722_18IMEElectricity { get; set; } public string _850766_85DustNoise1 { get; set; } public string _850772_63Dustnoise2 { get; set; } public string _850774_29DustNoise3 { get; set; } public string _863859_63Level { get; set; } public string _864233_30 { get; set; } public string _864319_07noise { get; set; } public IEnumerator<KeyValuePair<string, string>> GetEnumerator() { yield return new KeyValuePair<string, string>(nameof(_850347_74Crompton1), _850347_74Crompton1); yield return new KeyValuePair<string, string>(nameof(_850532_41Crompton2), _850532_41Crompton2); yield return new KeyValuePair<string, string>(nameof(_850722_18IMEElectricity), _850722_18IMEElectricity); yield return new KeyValuePair<string, string>(nameof(_850766_85DustNoise1), _850766_85DustNoise1); yield return new KeyValuePair<string, string>(nameof(_850772_63Dustnoise2), _850772_63Dustnoise2); yield return new KeyValuePair<string, string>(nameof(_850774_29DustNoise3), _850774_29DustNoise3); yield return new KeyValuePair<string, string>(nameof(_863859_63Level), _863859_63Level); yield return new KeyValuePair<string, string>(nameof(_864233_30), _864233_30); yield return new KeyValuePair<string, string>(nameof(_864319_07noise), _864319_07noise); } System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { return GetEnumerator(); } }
之后即可直接用foreach遍历:
Devices iot = config.GetRequiredSection("Devices").Get<Devices>(); foreach (var kvp in iot) { Console.WriteLine($"{kvp.Key}: {kvp.Value}"); }
内容的提问来源于stack exchange,提问作者Fandango68

