.NET 中的反射有什么用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/905450/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 12:44:59  来源:igfitidea点击:

What use is Reflection in .NET?

.netreflection

提问by Milen

Possible Duplicates:
When do you use reflection? Patterns/anti-patterns
What exactly is Reflection and when is it a good approach?

可能的重复:
你什么时候使用反射?模式/反模式
反射到底是什么,什么时候是一个好方法?

What is the need for reflection in .NET? What are the situations where it comes in handy?

.NET 中的反射需要什么?它在哪些情况下派上用场?

回答by BFree

Let's say you're writing a basic serialization routine, that will serialize any object to XML. How would you make it generic enough, so that it can work for any object? If you have a class where you know all the properties, then you can easily write a "ToXml()" function, where you manually write out all the properties to XML. What if you want to extend this though to ANY object? In that case, you need to reflect over the properties at runtime, and write them out to the XML.

假设您正在编写一个基本的序列化例程,它将任何对象序列化为 XML。你将如何使它足够通用,以便它可以用于任何对象?如果您有一个知道所有属性的类,那么您可以轻松编写一个“ToXml()”函数,您可以在其中手动将所有属性写出到 XML。如果你想把它扩展到任何对象怎么办?在这种情况下,您需要在运行时反映属性,并将它们写出到 XML。

There are many more uses for it, that's the first one that came to mind.

它还有很多用途,这是第一个想到的用途。

回答by JP Alioto

There are many uses for reflection. The .NET Framework uses it for serializationand for data binding, it can also be used for creating tools that examine your code like Reflector, FxCop and NUnit as well as ORM database frameworks. It has a wide variety of uses at runtime from logging specific things about an object to Dependency Injection Frameworks. It can also be used to dynamically execute methods or set properties at runtime as is done with custom attributes. It can also be used in higher level programming such as metaprogrammingand self-modifying code.

反射有很多用途。.NET Framework 将它用于序列化数据绑定,它还可以用于创建检查代码的工具,如 Reflector、FxCop 和 NUnit 以及 ORM 数据库框架。它在运行时具有广泛的用途,从记录有关对象的特定事物到依赖注入框架。它还可以用于在运行时动态执行方法或设置属性,就像使用自定义属性一样。它还可以用于更高级别的编程,例如元编程和自修改代码。

回答by James L

It's used for metaprogramming, which is when you're coding about code itself rather than the usual business problems

它用于元编程,即当您针对代码本身而不是通常的业务问题进行编码时

回答by Andrew Hare

I think the Wikipedia article on reflectionis pretty good:

我觉得维基百科上关于reflection的文章还不错:

In computer science, reflection is the process by which a computer program can observe and modify its own structure and behavior. The programming paradigm driven by reflection is called reflective programming. It is a particular kind of metaprogramming.

In many computer architectures, program instructions are stored as data - hence the distinction between instruction and data is merely a matter of how the information is treated by the computer and programming language. Normally, 'instructions' are 'executed' and 'data' is 'processed'; however, in some languages, programs can also treat instructions as data and therefore make reflective modifications. Reflection is most commonly used in high-level virtual machine programming languages like Smalltalk and scripting languages, and less commonly used in manifestly typed and/or statically typed programming languages such as Java and C.

在计算机科学中,反射是计算机程序观察和修改自身结构和行为的过程。由反射驱动的编程范式称为反射式编程。它是一种特殊的元编程。

在许多计算机体系结构中,程序指令被存储为数据——因此指令和数据之间的区别仅仅是计算机和编程语言如何处理信息的问题。通常,“指令”被“执行”,“数据”被“处理”;但是,在某些语言中,程序也可以将指令视为数据,从而进行反射性修改。反射最常用于高级虚拟机编程语言(如 Smalltalk 和脚本语言),较少用于显式类型和/或静态类型编程语言(如 Java 和 C)。

Reflection allows you to get information about the type of an object at execution time. This can be helpful in many situations including (but limited to) serialization and object-relational mappings.

反射允许您在执行时获取有关对象类型的信息。这在许多情况下都非常有用,包括(但限于)序列化和对象关系映射。

回答by ralphtheninja

It can also be very useful when testing your application, e.g. unit tests or other types of test frameworks. By using reflection you could for example create an xml file (or some other input data file in suitable format) and have your program analyze it and call methods that are defined in the file. This is how Fitnessework.

它在测试您的应用程序时也非常有用,例如单元测试或其他类型的测试框架。例如,通过使用反射,您可以创建一个 xml 文件(或一些其他合适格式的输入数据文件),并让您的程序分析它并调用文件中定义的方法。这就是Fitnesse 的工作方式。

回答by Davide Vosti

Reflection is used when you don't know at compile time which time your object is or what action to do or on what property...

当您在编译时不知道您的对象是什么时间或要执行什么操作或在什么属性上时,将使用反射...

You can use reflection to find all object of Type X in a given Assembly, or invoke some method by it's name (fetched from a db, or config file...), same thing for setting a property.... things like that (these are the case where we used it)

您可以使用反射在给定的程序集中查找所有类型 X 的对象,或者通过它的名称调用某个方法(从数据库或配置文件中获取...),设置属性也是如此...类似的事情(这些是我们使用它的情况)