• 使用PropertyInfo类反射获取类 的类型


    使用PropertyInfo类反射获取类 的类型

     

    首先构造一个泛型类

    public class ClassName<T>

    {

    }

    然后定义一个方法 方法返回集合

    view plaincopy to clipboardprint?
    public class Class1<T> 

        public IList<T> GetData(SqlDataReader reader) 
        { 
            IList<T> list = new List<T>(); 
            Type type = typeof(T); 
            PropertyInfo[] properties = type.GetProperties(); 
     
            while (reader.Read()) 
            { 
                T t = Activator.CreateInstance<T>(); 
                for (int i = 0; i < properties.Length; i++) 
                { 
                    properties[i].SetValue(t, reader[i + 1], null); 
     
                } 
     
                list.Add(t); 
            } 
     
            return list; 
        } 

        public class Class1<T>
        {
            public IList<T> GetData(SqlDataReader reader)
            {
                IList<T> list = new List<T>();
                Type type = typeof(T);
                PropertyInfo[] properties = type.GetProperties();

                while (reader.Read())
                {
                    T t = Activator.CreateInstance<T>();
                    for (int i = 0; i < properties.Length; i++)
                    {
                        properties[i].SetValue(t, reader[i + 1], null);

                    }

                    list.Add(t);
                }

                return list;
            }
        }

    上面给出了核心代码 如果你要传递sql语句

    那你的业务逻辑层 就要这一个方法也就够了!

     下面一个扩展方法 由 论坛的sql1234提供 在一次感叹 linq语法的简洁

    view plaincopy to clipboardprint?
    public static IEnumerable<T> GetObjects<T>(this DbDataReader rd) where T : new() 

        var fs = (from fd in typeof(T).GetFields() 
                    let desc = new { field = fd, index = rd.GetOrdinal(fd.Name) } 
                    where desc.index >= 0 
                    select desc) 
                .ToList(); 
        foreach (var x in rd) 
        { 
            var obj = new T(); 
            fs.ForEach(d => { d.field.SetValue(obj, rd[d.index]); }); 
            yield return obj; 
        } 

    public static IEnumerable<T> GetObjects<T>(this DbDataReader rd) where T : new()
    {
        var fs = (from fd in typeof(T).GetFields()
                    let desc = new { field = fd, index = rd.GetOrdinal(fd.Name) }
                    where desc.index >= 0
                    select desc)
                .ToList();
        foreach (var x in rd)
        {
            var obj = new T();
            fs.ForEach(d => { d.field.SetValue(obj, rd[d.index]); });
            yield return obj;
        }
    }

    这里,我们通过扩展方法,为任意DbDataReader都增加了一个GetObjects方法,返回任意指定类型的强类型的对象集合。
    如果包括private的field才更完整。应该将 GetFields() 修改为
    GetFields(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic)

  • 相关阅读:
    .NET 使用EF执行存储过程你知道几种?
    SQLserver 如何优雅的行转列
    SQLserver 如何获取近1月、近3个月、近6月数据
    三汇自动挂断问题:SIP兼容性,ACK检测,忽略ACK开启。
    几个flutter 开源项目测试
    Android versions for all users globally
    Using Flutter 2 on M1 MacOS Apple Silicon
    Educational Codeforces Round 111
    Wannafly挑战赛1
    摆烂记录
  • 原文地址:https://www.cnblogs.com/dzone/p/2002447.html
Copyright © 2020-2023  润新知