本文介绍了高达什么程度反思应该用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我们已经进入了一个非常棘手的场景的项目。我们已经在我们的项目中使用大量的反思。

We've got into a very tricky scenario in a project. We have used lot of reflection in our project.

我们有..

  • 将属性和反射驱动的验证框架
  • 的transaforms一个DataRow使用属性和思考,反之亦然实体对象扩展方法。同样的事情,我们已经做了的DataTable和EntityCollections。
  • 在通用EntityComparer类实现
  • 的Icomparable接口,使用反射来比较两个实体对象。
  • Validation Framework driven by Attributes and Reflection
  • Extension methods that transaforms a DataRow to an Entity Object using Attributes and Reflection and vice versa. The same thing we have done for DataTable and EntityCollections.
  • IComparer interface implemented on generic EntityComparer class that uses reflection to compare two Entity OBjects.

就像上​​面的方案中,我们使用了反思在我们的许多器件的应用等部位。利用反射后,我们已经注意到,该应用程序占用了过多的加工周期与反思。

Like above scenarios we have used Reflections at many other parts in our appication. After using the reflection we have noticed that the application is taking more processing cycles with reflection.

高达什么程度,我们应该在我们的项目中使用反射?什么是最不利反射处理方面影响了项目的领域?当反射不会做出任何性能影响?有没有使用反射的指引?

Upto what extent we should use Reflection in our project ? What are the areas of a project that are most adversely affected by reflection in terms of processing ? Where reflection will not make any performance impact ? Are there any guidelines on using Reflection ?

推荐答案

反射是强大的,但也有缺点。在一般情况下,尽量code无它 - 但它是框架库,在这里你几乎没有/有限的知识有什么实际的对象将是非常有用的;例如:

Reflection is powerful, but has drawbacks. In general, try to code without it - but it is hugely useful for "framework" libraries, where you have little/limited knowledge what the actual objects will be; for example:

  • 在ORM / DAL工具(加载/保存任意数据)
  • 数据绑定
  • 序列化

有有特设的反射性能损失,但是如果你要做到这一点的很多(你的库中,并在一个紧密的循环),可以减轻对这样的:

There are performance penalties with ad-hoc reflection, but if you are going to do it lots (within your library, and in a tight loop) you can mitigate against this:

  • 使用 Delegate.CreateDelegate (作为键入委托;不要使用 DynamicInvoke
  • 通过编写动态IL
  • 在.NET 3.5中使用防爆pression
  • by using Delegate.CreateDelegate (as a typed delegate; don't use DynamicInvoke)
  • by writing dynamic IL
  • by using Expression in .NET 3.5

当然,任何此类code应该被缓存和再利用(你不想编译+每次调用执行,只是第 - 其余的应该只是执行)。

And of course, any such code should be cached and re-used (you don't want to compile+execute for every call; just the first - the rest should just execute).

或者有库摘要;例如, HyperPropertyDescriptor 包装定制IL(会员访问)的熟悉的PropertyDescriptor API - 使得它非常快,但不痛。你的问题中提到的数据表和实体;这听起来很多这样previous问题,在那里我没有使用HyperDescriptor一些度量与总结(以毫秒为单位):

Or there are libraries that abstract this; for example, HyperPropertyDescriptor wraps custom IL (for member access) in the familiar PropertyDescriptor API - making it very fast but painless. Your question mentions data-tables and entities; which sounds a lot like this previous question, where I did some metrics using HyperDescriptor, with summary (in milliseconds):

Vanilla 27179
Hyper   6997

所以,我的走:

So my "take":

  • 应用程序;非常少 - 作为最后的手段,一般
  • 在您的公用库/库;在适当情况下(注意,接口等都是preferable如果可能)
  • in the application; very little - as a last resort, generally
  • in your common library/libraries; where appropriate (noting that interfaces etc are preferable where possible)

这篇关于高达什么程度反思应该用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-08 03:01