Skip to content

I don't understand Reflection in C#

Abhimanyu Aryan

Abhimanyu Aryan

Asked Jun 21, 2015, 8:52 PM EDT1 answer
Share
LinkedInX
On MSDN Reflection is explained as "You can use reflection to dynamically create an instance of a type, bind the type to an existing object, or get the type from an existing object and invoke its methods or access its fields and properties. "
 
 
 As an example they have stated: 
 
Python
int i = 42;
System.Type type = i.GetType();
System.Console.WriteLine(type);


If I am not wrong according to definition they have dynamically create 'type' object here and stored 'i.GetType()' return value in type. Is that what's reflection?

1 Answer

Know the answer? Post it. Somebody with the same question will find it here.

  • Manoj Bhoir
    +1
    Hi,
     
    Reflection objects are used for obtaining type information at runtime. The classes that give access to the metadata of a running program are in the System.Reflection namespace.
     
    In first line you are declaring variable i as int.
    int i = 42;
    Now, on second line in type variable it will store type of variable i.
    System.Type type = i.GetType();
    And when you will execute this code you will get output as
    System.Int32
    +1