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

cs 复制代码
/// <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();
    }
}

实体类转换方法

cs 复制代码
//将实体类集合转换为支持属性名称索引的类
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;
}

调用

cs 复制代码
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
相关推荐
AlunYegeer7 小时前
MyBatis 传参核心:#{ } 与 ${ } 区别详解(避坑+面试重点)
java·mybatis
乱蜂朝王7 小时前
使用 C# 和 ONNX Runtime 部署 PaDiM 异常检测模型
开发语言·c#
少许极端8 小时前
算法奇妙屋(四十)-贪心算法学习之路7
java·学习·算法·贪心算法
波诺波8 小时前
p1项目system_model.py代码
开发语言·python
危笑ioi8 小时前
helm部署skywalking链路追踪 java
java·开发语言·skywalking
夕除8 小时前
Mysql--15
java·数据库·mysql
静心观复8 小时前
Python 虚拟环境与 pipx 详解
开发语言·python
卷心菜狗8 小时前
Re.从零开始使用Python构建本地大模型网页智慧聊天机器人
开发语言·python·机器人
smileNicky8 小时前
Linux 系列从多节点的catalina 日志中统计设备调用频次
java·linux·服务器
书到用时方恨少!8 小时前
Python NumPy 使用指南:科学计算的基石
开发语言·python·numpy