反射和泛型的改进 [英] Improvements with reflection and generics

查看:110
本文介绍了反射和泛型的改进的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



我想澄清一些事情:


  1. 我假设通过反思获取房地产信息成本高昂?正确?

  2. 使用 PropertyInfo.SetValue(Object,Object,Object [])最后一个参数是什么?我是否安全地传递null?

  3. 是否有任何其他改进在我的代码中显而易见?

整个过程是一个学习练习,看看我能不能创造出像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 $ b $ pre code> private static Dictionary< Type,List< PropertyInfo>> PropertyCache {get;组; }


解决方案


  1. 获取属性信息通过反射是昂贵的,但通过反射访问属性也是如此。通过完全避免反思,您从这种方法中获得的收益几乎没有那么高。此外,除非您已确定此代码是性能瓶颈,否则大多数优化过程都为时过早。
  2. 使用PropertyInfo.SetValue(Object,Object,Object []),最后一个参数与索引属性(例如 dict [0] )实际访问一个索引属性有关,并且会传递一个数组 0 在第三个参数中)。
  3. 我建议使用 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:

  1. I am assuming that getting property information via reflection is costly? Correct?
  2. With PropertyInfo.SetValue(Object, Object, Object[]) what is the last parameter for? Am I safe passing in null?
  3. 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; }

解决方案

  1. 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.
  2. 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).
  3. I'd suggest using a ConcurrentDictionary for your PropertyCache. It will help to both simplify your code and make it thread-safe.

这篇关于反射和泛型的改进的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆