【.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
相关推荐
落鹜秋水25 分钟前
xss总结
开发语言·python
Littlewith1 小时前
Node.js:Stream、模块系统
java·服务器·开发语言·node.js·编辑器
名字不要太长 像我这样就好1 小时前
【iOS】消息传递和消息转发
开发语言·学习·macos·ios·objective-c
qq_366336371 小时前
JUC并发包CountDownLatch减法计数器的使用实例(多线程)
java·开发语言·算法
拉一次撑死狗2 小时前
Python绘制数据(二)
开发语言·python·信息可视化
屁股割了还要学2 小时前
【C语言进阶】题目练习(3)
c语言·开发语言·c++·学习·算法·青少年编程
白日依山尽yy2 小时前
SpringBoot项目部署至云服务器
java·spring boot·后端
典孝赢麻崩乐急2 小时前
Java学习--------消息队列的重复消费、消失与顺序性的深度解析
java·开发语言·学习
孤的心了不冷2 小时前
【后端】Linux系统发布.NetCore项目
.netcore
西猫雷婶3 小时前
python学智能算法(十九)|SVM基础概念-超平面
开发语言·人工智能·python·深度学习·算法·机器学习·支持向量机