2
Reply

What is reflection in C#? Give example.

Santosh Kumar

Santosh Kumar

Oct 05, 2013
33.6k
2

    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. The System.Reflection namespace contains classes that allow you to obtain information about the application and to dynamically add types, values and objects to the application. For learning about more in c# You can refer this link csharp interview questions

    Mohit Kumar
    April 07, 2014
    2

    Reflection provides objects (of typeType) that encapsulate assemblies,modules and types. You can usereflection to dynamically create aninstance of a type, bind the type toan existing object, or get the typefrom an existing object and invoke itsmethods or access its fields andproperties. If you are usingattributes in your code, Reflectionenables you to access them.
    For example, if you want to programmatically display all the methods of a class, you could do it like so:

    using System;
    using System.Reflection;namespace ConsoleApplication1 { class Program { static void Main(string[] args) { var t = typeof(MyClass); foreach (var m in t.GetMethods()) { Console.WriteLine(m.Name); } Console.ReadLine(); } } public class MyClass { public int Add(int x, int y) { return x + y; } public int Subtract(int x, int y) { return x - y; } } }

    Santosh Kumar
    October 05, 2013
    2