Click here to Skip to main content
15,892,643 members
Articles / Desktop Programming / MFC
Article

Reflector

Rate me:
Please Sign up or sign in to vote.
1.00/5 (13 votes)
11 Jan 2004 60.2K   1.6K   24   7
An article on Reflection.

Sample screenshot

Introduction

Reflector acts as a class browser of an assembly whether it may be an executable or any DLL. Here in the Reflector, we can see how to rive an assembly using Reflection in .NET.

OverView

This code uses FieldInfo to get the fields (what we call data members in C#) using GetFields(BindingFlags), PropertyInfo to get the properties (to get or set the fields) using GetProperties(BindingFlags), MethodInfo to the methods that work on the data using GetMethods(BindingFlags) and ParameterInfo to get the parameters that are passed using GetParameters().

CodeListing

To load an assembly:

C#
assembly = Assembly.LoadFrom(this.assemblyName);

To Load all the types in an assembly:

/// <summary>
/// Loads all the types contained in the loaded assembly
/// </summary>
/// <returns></returns>
public ArrayList LoadAllTypes()
{   
 AssemblyTypeDetails typeDetails = null;
 try
 {    
  Type []types = assembly.GetExportedTypes();    
  typeDetails = null;
  typeList = new ArrayList();
  foreach(Type type in types)
  {
   typeDetails = new AssemblyTypeDetails();
   typeDetails.TypeFullName = type.FullName;
   typeDetails.TypeName = type.Name;
   typeList.Add(typeDetails);
  }
 }
 catch(Exception ex)
 {
  throw new Exception(ex.StackTrace);
 }
 finally
 {
  typeDetails = null;    
 }
 return typeList;
}

To Load Fields:

C#
/// <summary>
/// returns an arraylist of the fields in the type 
/// </summary>
/// <param name="typeDetails">typeDetails</param>
/// <returns>array list of the fields</returns>
public ArrayList GetFields(AssemblyTypeDetails typeDetails)
{ 
 ArrayList fields = null;
 try
 {
  fields = new ArrayList();
  Type type = assembly.GetType(typeDetails.TypeFullName, true, true);
  if(type != null)
  {
   foreach(FieldInfo field in type.GetFields(BindingFlags.Public 
   | BindingFlags.Instance | BindingFlags.Static
   |BindingFlags.NonPublic |BindingFlags.DeclaredOnly))
     {
   fields.Add(field.Name.ToString()
   +" : "+field.Attributes.ToString()
   +" ["+field.FieldType.FullName+"]");
  }
 }    
 }
 catch(Exception ex)
 {
  throw new Exception(ex.Message);
 }
 return fields;
}

To Load Properties:

C#
/// <summary>
  /// Returns an arraylist of the properties
  /// </summary>
  /// <param name="typeDetails">typeDetails</param>
  /// <returns>arraylist of the properties</returns>
  public ArrayList GetProperties(AssemblyTypeDetails typeDetails)
  { 
   ArrayList props = null;
   try
   {
    props = new ArrayList();
    Type type = assembly.GetType(typeDetails.TypeFullName, true, true);
    if(type != null)
    {
     foreach(PropertyInfo prop in type.GetProperties(BindingFlags.Public 
      | BindingFlags.Instance
      | BindingFlags.Static 
      | BindingFlags.NonPublic
      |BindingFlags.DeclaredOnly))
     {
      props.Add(prop.Name.ToString()
       +" : "+prop.PropertyType.Name);
       
     }
    }    
   }
   catch(Exception ex)
   {
    throw new Exception(ex.Message);
   }
   return props;
  }

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralCould you reflector an dll or exe file which have a strong key name Pin
lpbinh21-Jul-05 15:32
lpbinh21-Jul-05 15:32 
I had read your paper and i wonder if could i reflector an dll or .exe file (writen by c#) which signature with a strong key name.

And i have another question.
Example i write a dll (mydll.dll) with have two function:
the first:
int Mul(int a, int b)
the second:
int div(int a, int b).

And i build it and signature it with a strong key name file is "mykey.sn"
such as:

[assembly: AssemblyDelaySign(true)]
[assembly: AssemblyKeyFile("mykey.sn")]

Now i build a window form application to using this dll (i mean call two function in mydll.dll) But i dont know how to using it.

I wonder if i can using in normally. I mean it similar with when i don't signature by strong key name.

[assembly: AssemblyDelaySign(false)]
[assembly: AssemblyKeyFile("")]

Could some one explain me ?
GeneralConfusing title Pin
Andre Seibel21-Oct-04 18:03
Andre Seibel21-Oct-04 18:03 
GeneralRe: Confusing title Pin
zaccheus24-Feb-07 23:35
zaccheus24-Feb-07 23:35 
Questionnamespaces? Pin
Stanimir_Stoyanov15-Feb-04 20:19
Stanimir_Stoyanov15-Feb-04 20:19 
GeneralCode in pre tags Pin
Jonathan de Halleux12-Jan-04 4:39
Jonathan de Halleux12-Jan-04 4:39 
GeneralRe: Code in pre tags Pin
Uwe Keim12-Jan-04 5:22
sitebuilderUwe Keim12-Jan-04 5:22 
GeneralRe: Code in pre tags Pin
dog_spawn12-Jan-04 6:36
dog_spawn12-Jan-04 6:36 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.