Wednesday, December 10, 2008

Reflection in C# - List of Class Name, Method Name

Reflection is way through which we can identify metadata about assembly runtime.
Example: We have a .Net Assembly file (.dll file), which consist of 2 Class Definition and 10 Method Names, We can get information about classes and method name list through reflection.

Few Examples of Reflection

Loading Assembly FileAssembly assem = Assembly.LoadFrom(sAssemblyFileName);

Get List of Class Name.
Type[] types = assem.GetTypes();

Get List of Method Name


foreach (Type cls in types)
{
try
{
//Add Namespace Name
arrl.Add(cls.FullName);

//Add Class Name
if(cls.IsAbstract)
arrl.Add("Abstract Class:" + cls.Name.ToString());
else if(cls.IsPublic)
arrl.Add("Public Class:" + cls.Name.ToString());
else if(cls.IsSealed)
arrl.Add("Sealed Class:" + cls.Name.ToString());


MemberInfo[] methodName = cls.GetMethods();
foreach (MemberInfo method in methodName)
{
if (method.ReflectedType.IsPublic)
arrl.Add("\tPublic - " + method.Name.ToString());
else
arrl.Add("\tNon-Public - " + method.Name.ToString());
}
}
catch (System.NullReferenceException)
{
Console.WriteLine("Error msg");
}
}


A Sample Application Showing Good Usage of Reflection in .Net
Following application shows how to identify list of Classes and Method in any .Net Assembly file with the help of .Net Reflection. For an example I am using Ajax Assembly filename to list all its classname and method name.



Step 1: Design a web form consisting of following controls.
Label – Shows text “Assembly File Name”
Textbox – Taking Input Assembly File Name.
Listbox – For displaying list of Class Names and Method Names
Button – On Button Click Event Display List of Class Name and Method Name

Step 2: Write following code on Button_Click Event.


private void btnListMethodName_Click(object sender, EventArgs e)
{
string sAssemblyFileName = txtAssemblyFileName.Text;

if (sAssemblyFileName.Length != 0)
{
Assembly assem = Assembly.LoadFrom(sAssemblyFileName);
Type[] types = assem.GetTypes();
ArrayList arrl = new ArrayList();
foreach (Type cls in types)
{
try
{
//Add Class Name
arrl.Add(cls.FullName);
if(cls.IsAbstract)
arrl.Add("Abstract Class:" + cls.Name.ToString());
else if(cls.IsPublic)
arrl.Add("Public Class:" + cls.Name.ToString());
else if(cls.IsSealed)
arrl.Add("Sealed Class:" + cls.Name.ToString());

MemberInfo[] methodName = cls.GetMethods();
foreach (MemberInfo method in methodName)
{
//arrl.Add("\t" + method.Name.ToString());
if (method.ReflectedType.IsPublic)
arrl.Add("\tPublic - " + method.Name.ToString());
else
arrl.Add("\tNon-Public - " + method.Name.ToString());
}
}
catch (System.NullReferenceException)
{
Console.WriteLine("Error msg");
}
}

//Dump Data to NotePad File
FileStream fs = new FileStream(@"c:\myData.txt", FileMode.Create, FileAccess.Write, FileShare.ReadWrite);
StreamWriter sw = new StreamWriter(fs);
for (int i = 0; i < arrl.Count; i++)
{
lstInfo.Items.Add(arrl[i].ToString());
sw.WriteLine(arrl[i].ToString());
}
sw.Flush();
sw.Close();
}
}

4 comments:

Anonymous said...

Hi,
when i try this application, show me error ReflectionTypeLoadException on line:
Type[] types = assem.GetTypes();
Can you help me, please? I have good dll.

jstuardo said...

Why do you do something like: method.Name.ToString()? Since Name is already a string, there is no need to do call ToString().

Billy Wagner said...

Don't be so nancy istuardo. It happens. I do it all the time. We get use to declaring the end result as string. It doesn't effect the outcome so why even say anything??

Billy Wagner said...

Istuardo why even mention that??? Coders often get use to declaring that string at the end. I know I do. It doesn't effect the outcome. To me your just being anal about nothing and I suspect your a newbie or at least given the post date at the time. I wonder if you have learned anything since then?

Most Recent Post

Community Updates

Subscribe Blog via Email

Enter your email address:



Disclaimers:We have tried hard to provide accurate information, as a user, you agree that you bear sole responsibility for your own decisions to use any programs, documents, source code, tips, articles or any other information provided on this Blog.
Page copy protected against web site content infringement by Copyscape