C#:Reflection - A Step Ahead Series

C#:Reflection - A Step Ahead Series


Introduction:


This is a continuation to my earlier post C#-The Language : A Step Ahead Series-Part-II
Simply, the mechanism through which one can access metadata information is called reflection. Namespace System.Reflection can be used to browse through the metadata information. Using reflection you can also dynamically invoke methods using System.Type.Invokemember.


/* This Example is a part of different
* examples shown in Book:
* C#2005 Beginners: A Step Ahead
* Written by: Gaurav Arora
* Reach at : msdotnetheaven*/

// File name : reflectionAssembly.cs

using System;
using System.Reflection;
namespace CSharp.AStepAhead.reflectionAssembly
{
public class reflectionAssembly
{
public static void Main()
{
Type myType = typeof(aClassToTest);
Console.Clear();
Console.WriteLine("\nType of class: " + myType);
Console.WriteLine("Namespace: " + myType.Namespace);
ConstructorInfo[] ci = myType.GetConstructors();
Console.WriteLine("\n-----------------------------------------------");
Console.WriteLine("Constructors are:");
foreach (ConstructorInfo i in ci)
{
Console.WriteLine(i);
}
PropertyInfo[] pi = myType.GetProperties();
Console.WriteLine("\n-----------------------------------------------");
Console.WriteLine("Properties are:");
foreach (PropertyInfo i in pi)
{
Console.WriteLine(i);
}
MethodInfo[] mi = myType.GetMethods();
Console.WriteLine("\n-----------------------------------------------");
Console.WriteLine("Methods are:");
foreach (MethodInfo i in mi)
{
Console.WriteLine("\nName: " + i.Name);
ParameterInfo[] pif = i.GetParameters();
foreach (ParameterInfo p in pif)
{
Console.WriteLine(" Type: " + p.ParameterType + " parameter name: " + p.Name);
}
}
Console.ReadLine();
}

public class aClassToTest
{
public int pubInteger;
private int _privValue;

public aClassToTest()
{
//Blank construtor
}

public aClassToTest(int newIntValue)
{
pubInteger = newIntValue;
}
public int Add(int num1, int num2)
{
return num1 + num2;
}
public int Substract(int num1, int num2)
{
return num1 - num2;
}
public int Multiply(int num1, int num2)
{
return num1 * num2;
}

public int privValue
{
get
{
return _privValue;
}
set
{
_privValue = value;
}
}
}
}
}



In above example, by simply specifying the name of a type, you can query almost all aspects of the object.
You can determine whether the object is a class, what its base system type is, and many other properties.


Article by Gaurav Aroraa
Gaurav is a Microsoft Technology Specialist professional. He has awarded lifetime membership from Computer Society of India (CSI). He has more than 13yrs of experience in the industry. Currently, he is working in the capacity of Solution Architect with an MNC. He is serving to the various communities since 1999.

Follow Gaurav Aroraa or read 157 articles authored by Gaurav Aroraa

Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: