反射和泛型的改进

编程入门 行业动态 更新时间:2024-10-21 09:08:38
本文介绍了反射和泛型的改进的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

继续阅读 Stack Overflow 的另一个问题一个不同的用户,我想我会尝试代码,看看我能不能改进它。

我想澄清一些事情:

  • 我假设通过反思获取房地产信息成本高昂?正确?
  • 使用 PropertyInfo.SetValue(Object,Object,Object [])最后一个参数是什么?我是否安全地传递null?
  • 是否有任何其他改进在我的代码中显而易见?

    整个过程是一个学习练习,看看我能不能创造出像Dapper (这非常好) 不知道) IL排放东西。有了这个想法,我试图实施某种缓存,并假设反射对性能不利。

    < (SqlDataReader行,List< PropertyInfo>属性,Dictionary< T>字符串,Int32>模式)其中T:new() { T item = new T(); if(schema!= null&& properties!= null&& row!= null) { foreach(属性中的var属性) { if(schema.ContainsKey(property.Name)) { //这可以吗? if(row [0] .GetType()== property.PropertyType) { property.SetValue(item,row [schema [property.Name]],null); { property.SetValue(item,Convert.ChangeType(row [schema [property.Name]],property.PropertyType),null); } } } } 退货项目; }

    这个属性是通过这个方法传入的:

    私人列表< PropertyInfo> GetPropertyInformation< T>() { List< PropertyInfo>性能; 类型_T = typeof(T); if(!PropertyCache.ContainsKey(_T)) { properties = _T.GetProperties()。ToList(); PropertyCache.Add(_T,properties); } else { properties = PropertyCache [_T]; } 返回属性;

    这是 PropertyCache private static Dictionary< Type,List< PropertyInfo>> PropertyCache {get;组; }

    解决方案

  • 获取属性信息通过反射是昂贵的,但通过反射访问属性也是如此。通过完全避免反思,您从这种方法中获得的收益几乎没有那么高。此外,除非您已确定此代码是性能瓶颈,否则大多数优化过程都为时过早。
  • 使用PropertyInfo.SetValue(Object,Object,Object []),最后一个参数与索引属性(例如 dict [0] )实际访问一个索引属性有关,并且会传递一个数组 0 在第三个参数中)。
  • 我建议使用 ConcurrentDictionary 来为你的 PropertyCache code>。这将有助于简化代码并使其线程安全。
  • Following on from another question on Stack Overflow by a different user, I thought I would try the code and see if I could improve it.

    I would like clarification on a number of things:

  • I am assuming that getting property information via reflection is costly? Correct?
  • With PropertyInfo.SetValue(Object, Object, Object[]) what is the last parameter for? Am I safe passing in null?
  • Is there any other improvements that are obvious in my code?
  • The whole point is a learning exercise to see if I can create something like Dapper (which is very nice) but without the nasty looking (because i have no idea) IL emit stuff. With this is mind, I tried to implement some sort of caching with the assumtion that reflection is bad for perfomance.

    The Code

    private T CreateItemFromRow<T>(SqlDataReader row, List<PropertyInfo> properties, Dictionary<String, Int32> schema) where T : new() { T item = new T(); if (schema != null && properties != null && row != null) { foreach (var property in properties) { if (schema.ContainsKey(property.Name)) { // is this ok? if (row[0].GetType() == property.PropertyType) { property.SetValue(item, row[schema[property.Name]], null); } else { property.SetValue(item, Convert.ChangeType(row[schema[property.Name]], property.PropertyType), null); } } } } return item; }

    The properties is passed in from this method:

    private List<PropertyInfo> GetPropertyInformation<T>() { List<PropertyInfo> properties; Type _T = typeof(T); if (!PropertyCache.ContainsKey(_T)) { properties = _T.GetProperties().ToList(); PropertyCache.Add(_T, properties); } else { properties = PropertyCache[_T]; } return properties; }

    And this is the declaration of PropertyCache

    private static Dictionary<Type, List<PropertyInfo>> PropertyCache { get; set; }

    解决方案

  • Getting property information via reflection is costly, but so is accessing the properties via reflection. You're not gaining nearly as much from this approach as you could gain by avoiding reflection altogether. Besides, unless you have determined that this code is a performance bottleneck, most optimization here will be premature anyway.
  • With PropertyInfo.SetValue(Object, Object, Object[]), the last parameter has to do with Index properties (e.g. dict[0] actually accesses an index property, and would pass an array with the 0 in that third parameter).
  • I'd suggest using a ConcurrentDictionary for your PropertyCache. It will help to both simplify your code and make it thread-safe.
  • 更多推荐

    反射和泛型的改进

    本文发布于:2023-11-16 23:57:53,感谢您对本站的认可!
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:反射

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!