In 3 minutes, What is Reflection?

前端 未结 12 1158
广开言路
广开言路 2020-12-02 10:33

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 questio

相关标签:
12条回答
  • 2020-12-02 11:18

    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.

    0 讨论(0)
  • 2020-12-02 11:22

    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.

    0 讨论(0)
  • 2020-12-02 11:23

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

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-02 11:27

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

    0 讨论(0)
  • 2020-12-02 11:29

    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 讨论(0)
提交回复
热议问题