如何慢是反思 [英] How slow is Reflection

查看:180
本文介绍了如何慢是反思的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近创建了一个接口层从业务逻辑层区分DataAccessProvider。
通过这种方法,每当我们想通过在Web / App.Config中改变的值,我们可以改变我们的选择DataAccessProvider的。
(可以给出更多的细节如果需要的话)。

I recently created an interface layer to distinguish the DataAccessProvider from our Business logic layer. With this approach we can change our choice of DataAccessProvider whenever we want by changing the values in the Web/App.Config. (more details can be given if needed).

总之,要做到这一点,我们使用反射来实现我们的DataProvider类上,我们可以工作。

Anyway, to do this we use reflection to accomplish our DataProvider class on which we can work.

/// <summary>
/// The constructor will create a new provider with the use of reflection.
/// If the assembly could not be loaded an AssemblyNotFoundException will be thrown.
/// </summary>
public DataAccessProviderFactory()
{
    string providerName = ConfigurationManager.AppSettings["DataProvider"];
    string providerFactoryName = ConfigurationManager.AppSettings["DataProviderFactory"];
    try
    {
    	activeProvider = Assembly.Load(providerName);
    	activeDataProviderFactory = (IDataProviderFactory)activeProvider.CreateInstance(providerFactoryName);
    }
    catch
    {
    	throw new AssemblyNotFoundException();
    }
}

但现在我不知道如何慢反射?

But now I'm wondering how slow reflection is?

推荐答案

在大多数情况下:比速度不够快了。例如,如果您要使用此创建一个DAL包装对象,所花费的时间来创建对象通过反射将微不足道相比,它需要连接到网络的时间。所以优化,这将是浪费时间。

In most cases: more than fast enough. For example, if you are using this to create a DAL wrapper object, the time taken to create the object via reflection will be minuscule compared to the time it needs to connect to a network. So optimising this would be a waste of time.

如果你是在一个紧密的循环使用反射,有技巧,以提高它:

If you are using reflection in a tight loop, there are tricks to improve it:


  • 泛型(使用一个包装其中T:新的() MakeGenericType

  • Delegate.CreateDelegate (到类型化的代表;不适用于构造工作)

  • Reflection.Emit的 - 铁杆

  • 防爆pression (如 Delegate.CreateDelegate ,但更灵活,适用于构造函数)

  • generics (using a wrapper where T : new() and MakeGenericType)
  • Delegate.CreateDelegate (to a typed delegate; doesn't work for constructors)
  • Reflection.Emit - hardcore
  • Expression (like Delegate.CreateDelegate, but more flexible, and works for constructors)

但你的目的,的CreateInstance 是完全没有问题。与坚持,并让事情变得简单。

But for your purposes, CreateInstance is perfectly fine. Stick with that, and keep things simple.

编辑:边长约相对表现点保持,虽然最重要的事情,衡量,仍然是,我要澄清上述一些。有时...它的确实的事情。第一项措施。不过,如果你发现它的的太慢,你可能想看看像 FastMember ,该完成所有的 Reflection.Emit的 code在后台悄悄,给你一个很好的方便的API;例如:

while the point about relative performance remains, and while the most important thing, "measure it", remains, I should clarify some of the above. Sometimes... it does matter. Measure first. However, if you find it is too slow, you might want to look at something like FastMember, which does all the Reflection.Emit code quietly in the background, to give you a nice easy API; for example:

var accessor = TypeAccessor.Create(type);
List<object> results = new List<object>();
foreach(var row in rows) {
    object obj = accessor.CreateNew();
    foreach(var col in cols) {
        accessor[obj, col.Name] = col.Value;
    }
    results.Add(obj);
}

这是简单的,但将是非常快的。在具体的例子我提到关于DAL包装,如果你正在做这个地段,可以考虑像短小精悍,这再次完成所有 Reflection.Emit的 code在后台给你以最快的速度,但易于使用的API:

which is simple, but will be very fast. In the specific example I mention about a DAL wrapper—if you are doing this lots, consider something like dapper, which again does all the Reflection.Emit code in the background to give you the fastest possible but easy to use API:

int id = 12345;
var orders = connection.Query<Order>(
    "select top 10 * from Orders where CustomerId = @id order by Id desc",
    new { id }).ToList();

这篇关于如何慢是反思的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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