Friday, June 5, 2009

Getting the Type of the current instance and Name of the method currently being executed

Hi Folks,

I am back, after a long time :) This post describes how we could get the name of the instance that is currently being executed and also the method name that is currently being executed.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Reflection;

namespace TestProject
{
class ToCall
{
public ToCall()
{
Console.WriteLine("This is " + this.GetType());
MethodBase method = MethodInfo.GetCurrentMethod();
string methodname = method.DeclaringType.FullName + "." + method.Name;
Console.WriteLine("Method name is " + methodname);

sample();
}

public void sample()
{
Console.WriteLine("This is " + this.GetType());
MethodBase method = MethodInfo.GetCurrentMethod();
string methodname = method.DeclaringType.FullName + "." + method.Name;
Console.WriteLine("Method name is " + methodname);
}
}

class Program
{
static void Main(string[] args)
{
ToCall d = new ToCall();
}
}
}


I will add some comments soon, just wanted to get started initially!!

Happy Coding :)

No comments: