Interviews2 answers
What is reflection in C#? Give example.
2 Answers
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 KumarPosted Apr 7, 2014, 2:01 AM EDT
Reflection provides objects (of type
Type) that encapsulate assemblies,
modules and types. 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. If you are using
attributes in your code, Reflection
enables you to access them.
For example, if you want to programmatically display all the methods of a class, you could do it like so:
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 KumarPosted Oct 5, 2013, 8:40 AM EDT
