在3分钟内,什么是反思?

时间:2021-08-04 18:17:03

Many .Net interview question lists (including the good ones) contain the question: "What is Reflection?". I was recently asked to answer this in the context of a 5 question, technical test designed to be completed in 15 minutes on a sheet of blank paper handed to me in a cafeteria. My answer went along the lines of "Reflection allows you to discover the Methods, Properties and Fields of any object at runtime". In retrospect, my answer explains how you can use reflection, but it does not explain what reflection is. In my view, my answer was sufficient to convey that I understand what reflection is for but didn't go so far as to explain what reflection is.

许多.Net访谈问题清单(包括好问题清单)都包含一个问题:“什么是反思?”。我最近被要求在5个问题的背景下回答这一问题,技术测试旨在15分钟内在一张自助餐厅递给我的空白纸上完成。我的回答是“反射允许您在运行时发现任何对象的方法,属性和字段”。回想起来,我的回答解释了如何使用反射,但它没有解释反射是什么。在我看来,我的答案足以表达我理解的反映是什么,但没有解释什么是反思。

So please, in the context of .Net, in your own concise words, define what Reflection is. Don't spend more than three minutes answering. Don't refer to the .Net documentation, we've all seen it.

所以,在.Net的上下文中,请用您自己的简洁词语来定义反射是什么。不要花三分多钟回答。不要参考.Net文档,我们都看过了。

12 个解决方案

#1


42  

Reflection is the ability to query and interact with the type system in a dynamic way.

反射是以动态方式查询类型系统并与之交互的能力。

#2


11  

a form of introspection i.e. the ability to write code that queries code

内省的一种形式,即编写查询代码的代码的能力

#3


9  

Reflection is the CLR's awareness of code-level objects such class names, methods, etc. that is exposed via an API, namely System.Reflection, which allows a developer to leverage the runtime's cognizance of this information in their code.

反射是CLR对代码级对象的认识,例如通过API公开的类名,方法等,即System.Reflection,它允许开发人员在其代码中利用运行时对这些信息的认知。

Rule violation: I've edited this answer from its original form for the sake of accuracy.

规则违规:为了准确起见,我从原始表格中编辑了这个答案。

#4


8  

Reflection is the ability of a program to handle itself as data.

反思是程序将自身作为数据处理的能力。

#5


6  

Reflection is like naval-gazing for code.

反思就像海军凝视代码。

#6


5  

During compilation of a .Net language, the compiler puts information about the program into the program file. This information can be used by the program itself or by other programs to find out which classes the program contains, what their methods, properties, fields and events are. Classes and their methods, properties and so on can also be used through reflection, even if the other program knows nothing about them before running. This allows different programs to be loosely coupled and makes all sorts of exciting programming possible. Reflection can also be used to build additional classes in running programs or in program files.

在编译.Net语言期间,编译器将有关程序的信息放入程序文件中。程序本身或其他程序可以使用此信息来查找程序包含哪些类,它们的方法,属性,字段和事件。类和它们的方法,属性等也可以通过反射来使用,即使其他程序在运行之前对它们一无所知也是如此。这允许不同的程序松散耦合并使各种令人兴奋的编程成为可能。 Reflection也可用于在运行程序或程序文件中构建其他类。

#7


4  

I like your answer but I would also mention that Reflection is also a way of getting/setting private/protected fields/properties, that would otherwise not be available at runtime.

我喜欢你的答案,但我还要提到,Reflection也是获取/设置私有/受保护字段/属性的一种方式,否则在运行时将无法使用。

#8


2  

Reflection is the Resume of Code.

反思是守则的简历。

#9


0  

Reflection is both metadata and Microsoft intermediate Language(MSIL) together wrapped in a portable Excutable(PE) file and this can be accessed at runtime by a mechanism.

反射是元数据和Microsoft中间语言(MSIL)一起包装在可移植的Excutable(PE)文件中,这可以在运行时通过机制访问。

#10


0  

Reflection is nothing but the ability to access method of other dll's which are not been included in your project (may be system or your own created) at the run time dynamically. It is also helpful to avoid circular dependency problems.

反射只不过是能够在运行时动态访问项目中未包含的其他dll的方法(可能是系统或您自己创建的)。避免循环依赖问题也很有帮助。

#11


0  

Reflection is the ability to act like a GOD (General Operations Director ;-)) - you can 'see' the internals of an assembly and do various acts (System.Reflection), specially designed for runtime, like querying for types and their members, search for interfaces or attributes, instantiate types not known at compile time and even create new types (System.Reflection.Emit).

反思就是像上帝一样行动的能力(通用运营总监;-)) - 你可以'看到'一个程序集的内部并做各种为行程专门设计的行为(System.Reflection),比如查询类型及其成员,搜索接口或属性,实例化编译时未知的类型,甚至创建新类型(System.Reflection.Emit)。

#12


0  

By using Reflection in C#, one is able to find out details of an object, method, and create objects and invoke methods at runtime.

通过在C#中使用Reflection,可以找到对象,方法和创建对象的详细信息,并在运行时调用方法。

using System;
using System.Reflection;

public class MyClass
{
   public virtual int AddNumb(int numb1,int numb2)
   {
     int result = numb1 + numb2;
     return result;
   }

}

class MyMainClass
{
  public static int Main()
  {
    // Create MyClass object
    MyClass myClassObj = new MyClass();
    // Get the Type information.
    Type myTypeObj = myClassObj.GetType();
    // Get Method Information.
    MethodInfo myMethodInfo = myTypeObj.GetMethod("AddNumb");
    object[] mParam = new object[] {5, 10};
    // Get and display the Invoke method.
    Console.Write("\nFirst method - " + myTypeObj.FullName + " returns " +  
                         myMethodInfo.Invoke(myClassObj, mParam) + "\n");
    return 0;
  }
}

below code will get the type information:

下面的代码将获取类型信息:

Type myTypeObj = Type.GetType("MyClass");

The code snippet below will get the method's information

下面的代码片段将获取方法的信息

Methodinfo myMethodInfo = myTypeObj.GetMethod("AddNumb"); 

The following code snippet will invoke the AddNumb method:

以下代码段将调用AddNumb方法:

myMethodInfo.Invoke(myClassObj, mParam);

#1


42  

Reflection is the ability to query and interact with the type system in a dynamic way.

反射是以动态方式查询类型系统并与之交互的能力。

#2


11  

a form of introspection i.e. the ability to write code that queries code

内省的一种形式,即编写查询代码的代码的能力

#3


9  

Reflection is the CLR's awareness of code-level objects such class names, methods, etc. that is exposed via an API, namely System.Reflection, which allows a developer to leverage the runtime's cognizance of this information in their code.

反射是CLR对代码级对象的认识,例如通过API公开的类名,方法等,即System.Reflection,它允许开发人员在其代码中利用运行时对这些信息的认知。

Rule violation: I've edited this answer from its original form for the sake of accuracy.

规则违规:为了准确起见,我从原始表格中编辑了这个答案。

#4


8  

Reflection is the ability of a program to handle itself as data.

反思是程序将自身作为数据处理的能力。

#5


6  

Reflection is like naval-gazing for code.

反思就像海军凝视代码。

#6


5  

During compilation of a .Net language, the compiler puts information about the program into the program file. This information can be used by the program itself or by other programs to find out which classes the program contains, what their methods, properties, fields and events are. Classes and their methods, properties and so on can also be used through reflection, even if the other program knows nothing about them before running. This allows different programs to be loosely coupled and makes all sorts of exciting programming possible. Reflection can also be used to build additional classes in running programs or in program files.

在编译.Net语言期间,编译器将有关程序的信息放入程序文件中。程序本身或其他程序可以使用此信息来查找程序包含哪些类,它们的方法,属性,字段和事件。类和它们的方法,属性等也可以通过反射来使用,即使其他程序在运行之前对它们一无所知也是如此。这允许不同的程序松散耦合并使各种令人兴奋的编程成为可能。 Reflection也可用于在运行程序或程序文件中构建其他类。

#7


4  

I like your answer but I would also mention that Reflection is also a way of getting/setting private/protected fields/properties, that would otherwise not be available at runtime.

我喜欢你的答案,但我还要提到,Reflection也是获取/设置私有/受保护字段/属性的一种方式,否则在运行时将无法使用。

#8


2  

Reflection is the Resume of Code.

反思是守则的简历。

#9


0  

Reflection is both metadata and Microsoft intermediate Language(MSIL) together wrapped in a portable Excutable(PE) file and this can be accessed at runtime by a mechanism.

反射是元数据和Microsoft中间语言(MSIL)一起包装在可移植的Excutable(PE)文件中,这可以在运行时通过机制访问。

#10


0  

Reflection is nothing but the ability to access method of other dll's which are not been included in your project (may be system or your own created) at the run time dynamically. It is also helpful to avoid circular dependency problems.

反射只不过是能够在运行时动态访问项目中未包含的其他dll的方法(可能是系统或您自己创建的)。避免循环依赖问题也很有帮助。

#11


0  

Reflection is the ability to act like a GOD (General Operations Director ;-)) - you can 'see' the internals of an assembly and do various acts (System.Reflection), specially designed for runtime, like querying for types and their members, search for interfaces or attributes, instantiate types not known at compile time and even create new types (System.Reflection.Emit).

反思就是像上帝一样行动的能力(通用运营总监;-)) - 你可以'看到'一个程序集的内部并做各种为行程专门设计的行为(System.Reflection),比如查询类型及其成员,搜索接口或属性,实例化编译时未知的类型,甚至创建新类型(System.Reflection.Emit)。

#12


0  

By using Reflection in C#, one is able to find out details of an object, method, and create objects and invoke methods at runtime.

通过在C#中使用Reflection,可以找到对象,方法和创建对象的详细信息,并在运行时调用方法。

using System;
using System.Reflection;

public class MyClass
{
   public virtual int AddNumb(int numb1,int numb2)
   {
     int result = numb1 + numb2;
     return result;
   }

}

class MyMainClass
{
  public static int Main()
  {
    // Create MyClass object
    MyClass myClassObj = new MyClass();
    // Get the Type information.
    Type myTypeObj = myClassObj.GetType();
    // Get Method Information.
    MethodInfo myMethodInfo = myTypeObj.GetMethod("AddNumb");
    object[] mParam = new object[] {5, 10};
    // Get and display the Invoke method.
    Console.Write("\nFirst method - " + myTypeObj.FullName + " returns " +  
                         myMethodInfo.Invoke(myClassObj, mParam) + "\n");
    return 0;
  }
}

below code will get the type information:

下面的代码将获取类型信息:

Type myTypeObj = Type.GetType("MyClass");

The code snippet below will get the method's information

下面的代码片段将获取方法的信息

Methodinfo myMethodInfo = myTypeObj.GetMethod("AddNumb"); 

The following code snippet will invoke the AddNumb method:

以下代码段将调用AddNumb方法:

myMethodInfo.Invoke(myClassObj, mParam);