为什么使用反射在.NET的建议? [英] Why is the use of reflection in .NET recommended?

查看:227
本文介绍了为什么使用反射在.NET的建议?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

绝对是它使用它一个很好的做法?

Is it definitely a good practice to use it?

什么是项目中的一些可能的情况下,需要反思?

What are some possible situations in a project that need reflection?

推荐答案

反射的主要价值在于,它可以被用来检查程序集,类型和成员。这是一个非常强大的工具,用于确定未知的组件或对象的内容,并可以在各种情况下使用。

The main value of Reflection is that it can be used to inspect assemblies, types, and members. It's a very powerful tool for determining the contents of an unknown assembly or object and can be used in a wide variety of cases.

反思的反对者会举出它是缓慢的,相对于静态的code执行这是真的 - 但是反射用于整个.NET框架,但它不被滥用它可以是一个非常强大的工具在工具包。

Opponents of Reflection will cite that it is slow, which is true when compared to static code execution--however Reflection is used throughout the .NET framework, and provided that it's not abused it can be a very powerful tool in the toolkit.

一些有用的应用程序:

  • 装配的确定依赖

  • Determining dependencies of an assembly

这符合一个接口位置的类型,从基/抽象类派生,并通过属性搜索成员

Location types which conform to an interface, derive from a base / abstract class, and searching for members by attributes

(臭臭)测试 - 如果你依赖于一类,它是无法验证的(即它不允许您轻松构建一个假的),你可以使用反射类中注入假的价值 - 它不是pretty的,不推荐,但它可以在绑定一个方便的工具。

(Smelly) testing - If you depend on a class which is untestable (ie it doesn't allow you to easily build a fake) you can use Reflection to inject fake values within the class--it's not pretty, and not recommended, but it can be a handy tool in a bind.

调试 - 倾出装组件,它们的引用,目前的方法,列表等...

Debugging - dumping out a list of the loaded assemblies, their references, current methods, etc...

这篇关于为什么使用反射在.NET的建议?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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