C#反射将静态类数据反射到字典并读取不同分支的字典数据

然后根据switch case匹配的字典加载

/// <summary>
/// 获取警报的端口状态
/// </summary>
    private Dictionary<uint, Dictionary<string, uint>> dictAlarmPort = new Dictionary<uint, Dictionary<string, uint>>();
for (uint i = 1; i <= 4; i++)
            {
                dictAlarmPort[i] = new Dictionary<string, uint>();
            }
            //RongDa 2022.11.21,将端口数据反射到字典中
            Type ts300 = typeof(TS300PortStatusDefine);
            Type ts480 = typeof(TS480PortStatusDefine);
            System.Reflection.FieldInfo[] ts300fis = ts300.GetFields();  // 注意,这里不能有任何选项,否则将无法获取到const常量
            System.Reflection.FieldInfo[] ts480fis = ts480.GetFields();
            string fileName = "RunSheng\profile.ini";
            Dictionary<string, string> setting = LDUtils.ReadAppDataIni(fileName);
            if(setting==null)
            {
                foreach (var fieldInfo in ts300fis)
                {
                    dictAlarmPort[1][fieldInfo.Name] = Convert.ToUInt32(fieldInfo.GetValue(null));
                }
            }
            else
            {
                for (uint extNo = 1; extNo <= 4; ++extNo)
                {
                    //string extKey = $"ExtNo{extNo}";
                    string extKey = $"DeviceType.ExtNo{extNo}";
                    if (setting.ContainsKey(extKey))
                    {
                        string devType = setting[extKey];
                        switch (devType.ToUpper())
                        {
                            case "TS480":
                                foreach (var fieldInfo in ts480fis)
                                {
                                    dictAlarmPort[extNo][fieldInfo.Name] = Convert.ToUInt32(fieldInfo.GetValue(null));
                                }
                                break;
                            case "C3000":
                            case "TS300":
                            case "RUN3000Y":
                                foreach (var fieldInfo in ts300fis)
                                {
                                    dictAlarmPort[extNo][fieldInfo.Name] = Convert.ToUInt32(fieldInfo.GetValue(null));
                                }
                                break;
                        }
                    }
                }
            }

2181398208153953

2c381cfad5154119

 

相关推荐: C#反射将静态类数据反射到字典并读取不同分支的字典数据

然后根据switch case匹配的字典加载/// <summary> /// 获取警报的端口状态 /// </summary> private Dictionary<uint, Dictionary<string, uin…

© 版权声明
THE END
喜欢就支持一下吧
点赞9 分享
评论 抢沙发
头像
欢迎您留下宝贵的见解!
提交
头像

昵称

取消
昵称表情代码图片

    暂无评论内容