[C#]反射的实战应用,实际数据模拟

csharp 复制代码
long? value = null;
// 看它是不是 HEX_STRING
var dtAttr = prop.GetCustomAttribute<DataTypeAttribute>();
if (dtAttr != null && dtAttr.DataType == DataType.HEX_STRING)
{
    // 去掉可能的 "0x" 前缀
    string txt = attribute.Value.StartsWith("0x", StringComparison.OrdinalIgnoreCase)
                 ? attribute.Value.Substring(2)
                 : attribute.Value;
    // 按十六进制解析
    if (long.TryParse(txt, NumberStyles.HexNumber, CultureInfo.InvariantCulture, out long hexResult))
        value = hexResult;
}
else
{
    // 按十进制解析
    if (long.TryParse(attribute.Value, NumberStyles.Integer, CultureInfo.InvariantCulture, out long decResult))
        value = decResult;
}
  1. 遍历 XML 里每个属性(XAttribute),
  2. 反射 找到同名的 GlobalParameter 属性 (PropertyInfo),
  3. 根据那个属性上有没有标记 [DataType(DataType.HEX_STRING)] 来决定"按十六进制"还是"按十进制"解析它的字符串值,
  4. 最后用 prop.SetValue(...) 把解析出来的 long? 写回到 globalParameter 对象里。

关键解析

csharp 复制代码
var prop = type.GetProperty(attribute.Name.LocalName);
if (prop == null || !prop.CanWrite)
    continue;
  • typetypeof(GlobalParameter)attribute.Name.LocalName 比如 "P00A""P100"......

  • GetProperty("P00A") 就拿到 GlobalParameter.P00APropertyInfo

  • prop.CanWrite 确保它有公开的 setter,否则无需赋值。

  • GetCustomAttribute<DataTypeAttribute>() 读出你在属性上贴的 [DataType(DataType.HEX_STRING)]

  • 如果它是 HEX_STRING,就把字符串(可能以 "0x" 开头)切掉前缀,再用 NumberStyles.HexNumber 按 16 进制转成 long

  • 否则直接按 10 进制 NumberStyles.Integer 转。

csharp 复制代码
prop.SetValue(globalParameter, value);
pnNotExists = false;
  • 这一行最关键:等同于 动态执行 globalParameter.P00A = value;globalParameter.P100 = value;
  • .NET 在后台会调用对应属性的 set 方法,把刚刚解析好的 long? 写进去。

举例模拟

假设 XML 节点是:

xml 复制代码
<GlobalParameter
    P00A="0x000B"
    P100="2500"
/>
  • 第一次循环

    • attribute.Name = "P00A", attribute.Value = "0x000B"
    • prop 对应 GlobalParameter.Pn00A,它有 [DataType(HEX_STRING)]
    • 去前缀后 txt = "000B"TryParse(..., HexNumber)hexResult = 11
    • prop.SetValue(globalParameter, 11) ------ 相当于 globalParameter.P00A = 11;
  • 第二次循环

    • attribute.Name = "Pn100", attribute.Value = "2500"
    • prop 对应 GlobalParameter.P100,默认是 INT
    • TryParse("2500", Integer)decResult = 2500
    • prop.SetValue(globalParameter, 2500) ------ 相当于 globalParameter.P100 = 2500;

最后返回的 globalParameter 对象里,

csharp 复制代码
globalParameter.P00A == 11;   // 0x000B → 11
globalParameter.P100 == 2500; // 2500

正因为 SetValue 就是调用属性的 setter,所以新解析出来的值就"自动"存到那个实例里了。

相关推荐
zzywxc78712 分钟前
详细探讨AI在金融、医疗、教育和制造业四大领域的具体落地案例,并通过代码、流程图、Prompt示例和图表等方式展示这些应用的实际效果。
开发语言·javascript·人工智能·深度学习·金融·prompt·流程图
浮灯Foden25 分钟前
算法-每日一题(DAY13)两数之和
开发语言·数据结构·c++·算法·leetcode·面试·散列表
淡海水39 分钟前
【原理】Struct 和 Class 辨析
开发语言·c++·c#·struct·class
淡海水42 分钟前
【原理】Unity GC 对比 C# GC
unity·c#·gc·垃圾回收
Q_Q196328847543 分钟前
python的电影院座位管理可视化数据分析系统
开发语言·spring boot·python·django·flask·node.js·php
杜子不疼.1 小时前
《Python学习之第三方库:开启无限可能》
开发语言·python·学习
张人玉1 小时前
C#读取文件, IO 类属性及使用示例
microsoft·c#
青川入梦2 小时前
MyBatis极速通关上篇:Spring Boot环境搭建+用户管理实战
java·开发语言·mybatis
CC__xy2 小时前
04 类型别名type + 检测数据类型(typeof+instanceof) + 空安全+剩余和展开(运算符 ...)简单类型和复杂类型 + 模块化
开发语言·javascript·harmonyos·鸿蒙
萤丰信息2 小时前
技术赋能安全:智慧工地构建城市建设新防线
java·大数据·开发语言·人工智能·智慧城市·智慧工地