43

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.

DOK
  • 32,337
  • 7
  • 60
  • 92
grenade
  • 31,451
  • 23
  • 97
  • 126
  • 2
    "Don't spend more than 3 minutes, don't refer to documentation" . . . don't ask questions like this. Voting to close. – Binary Worrier Dec 07 '09 at 13:23
  • 7
    You can close it if you want. I already got some good answers and that's why I asked the question. – grenade Dec 07 '09 at 13:25
  • @Binary Worrier, I may have been misleading with my don't refer statement. I mean that the answer shouldn't be "read the docs at some-url" rather than don't use the documentation to make a good answer. – grenade Dec 07 '09 at 13:42
  • 21
    I'm voting to reopen. This question has 5 good answers with upvotes. That says that at least a few people see it as a real question. – grenade Dec 07 '09 at 15:13
  • Reflections is, not fun. Meta program only if absolutely necessary. – nawfal Feb 12 '13 at 10:46

12 Answers12

46

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

Brian Rasmussen
  • 114,645
  • 34
  • 221
  • 317
  • That's nice! Very concise and "dynamic" says a lot. – grenade Dec 07 '09 at 13:20
  • 3
    But then, is a method's code part of the type system? It seems to me that you interact with more than just the type system – Vinko Vrsalovic Dec 07 '09 at 13:23
  • 1
    @Vinko: Methods are defined in the context of types, so I would say yes, but I see your point. To be honest I didn't expect to cover all the details of reflection in a single sentence. – Brian Rasmussen Dec 07 '09 at 13:26
  • 1
    It's concise, but it is very close to the original answer in the text of the question. – Guge Dec 07 '09 at 13:26
  • 2
    @Guge: Reflection is more than discovery. You can invoke methods and get/set fields as well, which is what I tried to cover with "interact". – Brian Rasmussen Dec 07 '09 at 13:29
  • How has nobody noticed that your answer exceeded the question's time limit by 1 minute? :) – BoltClock Jun 25 '13 at 06:56
13

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

jk.
  • 13,817
  • 5
  • 37
  • 50
  • 5
    +1 IMO most understandable answer - now I see why it is called `reflection` - like looking in a mirror. – jacknad Jun 01 '12 at 16:55
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.

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

antik
  • 5,282
  • 1
  • 34
  • 47
8

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

Amnon
  • 7,652
  • 2
  • 26
  • 34
6

Reflection is like naval-gazing for code.

Brian Ensink
  • 11,092
  • 3
  • 50
  • 63
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.

Guge
  • 4,569
  • 4
  • 35
  • 47
  • -1: Doesn't actually explain reflection. Sure, it's possible to get the classes, methods, etcetera, via reflection, but it is a pain to do so. It is much simpler to use them statically. Reflection, however, can be used for runtime, rather than compile time discovery of types and their methods. – Arafangion Jan 06 '12 at 11:56
  • 1
    Arafangion, my answer contains what reflection is, how it works, how it can be used, and I think my answer actually includes what you suggest in your comment. I suggest you read answers before you downvote them. – Guge Jun 04 '12 at 11:13
  • Guge: Strictly speaking, yes, however it doesn't really point how the practical implications nor the difference between compile-time and run-time reflection, and I disagree how it allows loose coupling. (In the context of .NET, it makes no difference with respect to coupling). Infact, much of what you point out could be done with C and the dynamic linker. (You're right, however, this was really a picky response of mine... Probably not worth the downvote, however, downvotes barely impact reputation) – Arafangion Jun 05 '12 at 02:38
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.

tzup
  • 3,566
  • 3
  • 26
  • 34
  • Reflection does enable this but that's not one of Reflection's redeeming qualities IMO. Fields are set to private for a reason: you're violating encapsulation using Reflection as such so it should be avoided if at all possible. Also, this answer is how to use reflection, not what reflection is. – antik Dec 07 '09 at 13:30
  • I would usually agree with your reasoning, but also think about the situation where you are working with a 3rd party component that you have no other access but by hacking into with Reflection. And yes, you're right, I just wanted to mention something that has not been said in the 5 previous answers. I'd rather add something unsaid to the thread instead of repeatingm, even if this means diverging a bit from the question...as long as I can add value to the discussion :) – tzup Dec 07 '09 at 13:44
  • 1
    I would agree it's not pretty but sometimes you really need something from a third party control. I found a memory leak in one and they wouldn't patch it for months so used reflection to temporarily do it myself. – PeteT Dec 09 '09 at 16:27
2

Reflection is the Resume of Code.

Gilligan
  • 1,505
  • 2
  • 15
  • 16
1

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.

Fiseha
  • 11
  • 1
1

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.

rhatwar007
  • 343
  • 3
  • 14
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).

Shahar G.
  • 1,440
  • 12
  • 22
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.

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);
Anshul Goyal
  • 73,278
  • 37
  • 149
  • 186
Nikki
  • 409
  • 1
  • 5
  • 15