Extracting Type Information using Reflection

muralikrishnasurap-12704
Posted by muralikrishnasurap-12704 under C# category on | Points: 40 | Views : 509
Hi Here I want to explain what is Reflection and how C# uses reflections classes to extract type information at run time.

Reflection is used to get type information at run time,System.Reflection namespace had bunch of class which are used to get the type information at run time.

Check the below example

namespace ReflectionNameSpace
{

class Program
{

static void Main(string[] args)
{
Type _type = typeof(TestClass);
FieldInfo[] _filedInfo = _type.GetFields();
foreach (FieldInfo field in _filedInfo )
{
Console.WriteLine("Display fields name" + field+"\n");
}

MethodInfo[] _methodinfo = _type.GetMethods();
foreach (MethodInfo method in _methodinfo)
{
Console.WriteLine("Method name: " + method + "\n");
}
Console.Read();
}
}
class TestClass
{
public decimal EMPSalary = 0;
public string EMPName = "EmployeName";

public void PublicMethod()
{
}
public void PrivateMethod()
{

}
public void InternalMethod()
{

}
public void ProtectedMethod()
{

}
}
}

Comments or Responses

Login to post response