【.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
相关推荐
温轻舟1 天前
Python自动办公工具01-Excel文件编辑器
开发语言·python·编辑器·excel·温轻舟
爱笑的眼睛111 天前
PyTorch Lightning:重新定义深度学习工程实践
java·人工智能·python·ai
Kevinyu_1 天前
责任链模式
java·hadoop·责任链模式
明洞日记1 天前
【设计模式手册012】责任链模式 - 请求处理的流水线艺术
java·设计模式·责任链模式
q***07141 天前
Java实战:Spring Boot application.yml配置文件详解
java·网络·spring boot
雨中飘荡的记忆1 天前
Spring Alibaba AI 实战指南
java·ai编程
冰封剑心1 天前
MiniCPM-V-2_6 (4-bit 量化)使用
java·前端·数据库
mqiqe1 天前
【Spring AI MCP】四、MCP 服务端
java·人工智能·spring
l***74941 天前
springboot与springcloud对应版本
java·spring boot·spring cloud
纵有疾風起1 天前
C++——多态
开发语言·c++·经验分享·面试·开源