什么是反射以及它在 C# 中的用途是什么?
可能的重复:
反思。使用它我们可以实现什么?
我看到很多在 c# 中使用反射的示例,但我不确定它在 c# 中主要用于什么。那么什么时候使用它呢?
Possible Duplicate:
Reflection. What can we achieve using it?
I see plenty of examples of using reflection in c#, but I'm not sure exactly what all it is mainly used for in c#. So when do you use it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
一个真实的案例:
一个函数在传递名称空间的名称时会查找名称空间中的所有类,如果它在调用它的类中找到函数“SelfTest”,则在需要时实例化一个对象。
这让我可以将测试函数声明为对象的一部分,而不必担心维护测试列表。
A real-world case:
A function that when passed the name of the namespace looks through all classes in the namespace and if it finds a function "SelfTest" in the class it calls it, instantiating an object if need be.
This lets me declare test functions as part of the objects and not have to worry about maintaining a list of tests.
有很多方法可以使用它。我使用它的一种方法是在单元测试中,当我需要损坏某些私有变量以使单元测试失败(以模拟故障测试场景)时。例如,如果我想模拟数据库连接失败,那么我可以使用下面的方法来更改与数据库一起使用的类中的
connectionString
私有变量。当我尝试连接到数据库时,这会导致数据库连接失败,并且在我的单元测试中我可以验证是否引发了正确的异常。前任:
There are lots of ways to use it. One way I use it is in unit testing when I need to corrupt some private variable to get a unit test to fail (to simulate failure test scenarios). For example, if I want to mock having a database connection fail, then I could use the below method to change the
connectionString
private variable in a class that works with the DB. This would cause the DB connection to fail when I tried to connect to the DB, and in my unit test I can verify that the proper exception is thrown.Ex:
反射是一种允许开发人员在运行时访问类型/实例的元数据的技术。
最常见的用法是定义 CustomAttribute 并在运行时使用它。 CustomAttribute已用于ORM、ASP.Net ActionFilter、单元测试框架等。Cory
Charlton在这个问题中回答得很好:
Relection is a technique which allow developers to access Metadata of the type/instance at runtime.
The most common usage is defining CustomAttribute and use it at run time. CustomAttribute has been used in ORMs, ASP.Net ActionFilter, Unit Testing Framework, etc.
Cory Charlton answered very well in this question:
Reflection. What can we achieve using it?
一般来说,任何涉及 System.Type 类型的东西都可以被视为反射。它对于各种约定优于配置场景通常很有用(除其他外)。
考虑一个示例,您想要创建一个直到运行时才知道的类型的实例:
In general anything that touches
System.Type
type can be considered reflection. It is often useful(amongst other things) for all sorts of Convention over Configuration scenarios.Consider an example where you want to create an instance of a type that you don't know until runtime:
http://msdn.microsoft.com/en-us /library/ms173183(VS.80).aspx
http://msdn.microsoft.com/en-us/library/ms173183(VS.80).aspx
例如,了解 Microsoft 如何在 web.config 中使用它:)
当我必须从 Autocompletebox 中过滤项目(使用 ItemFilter 属性)时,我使用了它。 ItemSource 是使用 Linq 设置的。由于每个项目都是 AnonymousType,因此我使用 Reflection 来获取属性并执行我想要的过滤器。
See how Microsoft uses it in a web.config for example :)
I used it when I have to filter items (using ItemFilter property) from Autocompletebox. The ItemSource was setted with Linq. As each item was of AnonymousType, I used Reflection to get properties and perform the filter I desire.
我使用它进行验证/编码,例如在发送到 Web 视图之前查看类中的所有字符串并将它们更改为 HTML 安全字符串。类似地,当从视图检索数据时,我运行编码/正则表达式以确保仅使用安全的 html 字符。
另一种方法是用 C# 编写插件,您希望在运行时了解其功能。代码项目示例: http://www.codeproject.com/KB/cs/pluginsincsharp .aspx
I use it for validation/encoding, like looking through all the strings in a class and changing them to HTML safe strings before I send over to a web view. Similary when retrieving the data from the view i run though a encoding/regex to make sure only safe html characters are used.
The other way is to write plugins in C# where you want the functionality to be known at runtime. example from code project: http://www.codeproject.com/KB/cs/pluginsincsharp.aspx
我用它来编译 Web 应用程序页面(来自完全独立的页面)内的控件列表。
它可以用来动态实例化类、分析程序集、类型检查……
正如它所说,反射,它允许程序查看自身。
I've used it to compile lists of controls within pages (from a totally separate page) of a web application.
It can be used to dynamically instantiate classes, analyze assemblies, type checking...
It is what it says, a reflection, it allows a program to look at itself.