当前位置: 首页 > news >正文

【.net core】支持通过属性名称索引的泛型包装类

/// <summary>
/// 支持通过属性名称索引的泛型包装类
/// </summary>
public class PropertyIndexer<T> : IEnumerable<T>
{private T[] _items;private T _instance;private PropertyInfo[] _properties;private bool _caseSensitive;public PropertyIndexer(T item, bool caseSensitive = true){_instance = item;_caseSensitive = caseSensitive;_properties = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);}// 通过属性名称获取值public object this[string propertyName]{get{PropertyInfo property = FindProperty(propertyName);if (property == null)throw new ArgumentException($"找不到属性: {propertyName}");return property.GetValue(_instance);}set{PropertyInfo property = FindProperty(propertyName);if (property == null)throw new ArgumentException($"找不到属性: {propertyName}");// 类型转换object convertedValue = Convert.ChangeType(value, property.PropertyType);property.SetValue(_instance, convertedValue);}}// 查找属性(支持大小写敏感/不敏感)private PropertyInfo FindProperty(string propertyName){if (_caseSensitive){return Array.Find(_properties, p => p.Name == propertyName);}else{return Array.Find(_properties, p => p.Name.Equals(propertyName, StringComparison.OrdinalIgnoreCase));}}// 获取底层实例public T Instance => _instance;// 实现IEnumerable<T>接口public IEnumerator<T> GetEnumerator(){foreach (T item in _items){yield return item;}}// 显式实现非泛型接口IEnumerator IEnumerable.GetEnumerator(){return GetEnumerator();}
}

实体类转换方法

//将实体类集合转换为支持属性名称索引的类
public static List<PropertyIndexer<T>> ConvertToPropertyIndexer<T>(List<T> dataList) {List<PropertyIndexer<T>> result = new List<PropertyIndexer<T>>();foreach (T item in dataList) {result.Add(new PropertyIndexer<T>(item));}return result;
}
//索引类转换为实体类
public static List<T> ConvertToPropertyIndexer<T>(List<PropertyIndexer<T>> dataList)
{List<T> result = new List<T>();foreach (PropertyIndexer<T> item in dataList){result.Add(item.Instance);}return result;
}

调用

public ClassA{public string name {get;set;}public string value {get;set;}
}List<ClassA> list = new List<ClassA>(){new ClassA(){name = "a",value="1"  },new ClassA(){name = "b",value="2"  },
}List<PropertyIndexer<ClassA>> values =DataCountCenterService.ConvertToPropertyIndexer<ClassA>(list);Console.WriteLine(values[0]["name"].ToString());//索引取字段值的返回结果是object,需要进行转换,此处输出结果为a

http://www.dtcms.com/a/286402.html

相关文章:

  • Spring AI开发智能客服(Tool calling)
  • Linux 定时任务全解析:atd 与 crond 的区别及实战案例(含日志备份 + 时间写入)
  • SpringBoot项目创建,三层架构,分成结构,IOC,DI相关,@Resource与@Autowired的区别
  • 融合优势:SIP 广播对讲联动华为会议 全场景沟通响应提速​
  • 【PHP】Swoole:CentOS安装MySQL+Swoole
  • 强化学习框架VeRL全面解析(架构、调试、修改与应用)
  • vbox增加虚拟磁盘空间大小
  • 基于springboot+vue+mysql的在线文档管理系统的设计与实现(源码+论文+PPT答辩+开题报告)
  • ClickHouse物化视图避坑指南:原理、数据迁移与优化
  • ESP32-IDF LVGL UI 设计工具的使用
  • 海森矩阵(Hessian Matrix)在SLAM图优化和点云配准中的应用介绍
  • Go 的热重载工具 Air 详解
  • 深入理解 Spring:事务管理与事件机制全解析
  • 域名WHOIS信息查询免费API使用指南
  • 【CF】⭐Day104——Codeforces Round 840 (Div. 2) CE (思维 + 分类讨论 | 思维 + 图论 + DP)
  • 【LVGL】Linux LVGL程序几十分钟后UI卡死
  • ubuntu 安装zabbix6 agent2
  • AI进入自动驾驶时代:OpenAI发布革命性ChatGPT Agent
  • 生成式引擎优化(GEO)核心解析:下一代搜索技术的演进与落地策略
  • OpenAI最强ChatGPT智能体发布:技术突破与应用前景分析
  • 脉冲神经网络(Spiking Neural Network, SNN)与知识蒸馏(Knowledge Distillation, KD)
  • 有好内容,如何做好知识变现?
  • BIST会对锁步核做什么?
  • 深入了解直播美颜SDK:GPU加速下的美白滤镜功能实现?
  • 解决 IDEA 中 XML 文件的 “URI is not registered” 报错
  • html5+css3+canvas纯前端4字方形LOGO生成器
  • 【C# in .NET】17. 探秘类成员-构造函数与析构函数:对象生命周期管理
  • Beagle 480 USB分析仪
  • 差分数组算法
  • 柴油机活塞cad【4张】三维图+设计说明书