Q:

What is Reflection in C#?

0

What is Reflection in C#?

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Reflection is the process of runtime type discovery to inspect metadata, CIL code, late binding, and self-generating code. At the run time by using reflection, we can access the same “type” information as displayed by the ildasm utility at design time. The reflection is analogous to reverse engineering in which we can break an existing *.exe or *.dll assembly to explore defined significant contents information, including methods, fields, events, and properties.
 

You can dynamically discover the set of interfaces supported by a given type using the System.Reflection namespace.

 

Reflection typically is used to dump out the loaded assemblies list, their reference to inspect methods, properties etcetera. Reflection is also used in the external disassembling tools such as Reflector, Fxcop, and NUnit because .NET tools don’t need to parse the source code similar to C++.

 

Metadata Investigation 

 

The following program depicts the process of reflection by creating a console-based application. This program will display the details of the fields, methods, properties, and interfaces for any type within the mscorlib.dll assembly. Before proceeding, it is mandatory to import “System.Reflection”.

 

Here, we are defining a number of static methods in the program class to enumerate fields, methods, and interfaces in the specified type. The static method takes a single “System.Type” parameter and returns void.

static void FieldInvestigation(Type t)
{
    Console.WriteLine("*********Fields*********");
    FieldInfo[] fld = t.GetFields();
    foreach(FieldInfo f in fld)
    {
        Console.WriteLine("-->{0}", f.Name);
    }
}
static void MethodInvestigation(Type t)
{
    Console.WriteLine("*********Methods*********");
    MethodInfo[] mth = t.GetMethods();
    foreach(MethodInfo m in mth)
    {
        Console.WriteLine("-->{0}", m.Name);
    }
}

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now