Santosh Kumar
What is reflection in C#? Give example.
By Santosh Kumar in C# on Oct 05 2013
  • Mohit Kumar
    Apr, 2014 7

    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

    • 2
  • Santosh Kumar
    Oct, 2013 5

    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; } } }

    • 2


Most Popular Job Functions


MOST LIKED QUESTIONS