开发者

In 3 minutes, What is Reflection?

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.

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.


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


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


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.

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


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


Reflection is like naval-gazing for code.


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.


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 is the Resume of Code.


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.


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.


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).


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

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:

myMethodInfo.Invoke(myClassObj, mParam);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜