Click here to Skip to main content
Click here to Skip to main content

Reflector

, 11 Jan 2004
Rate this:
Please Sign up or sign in to vote.
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:

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:

/// <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:

/// <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

Share

About the Author

Jayakrishna Damarla
Web Developer
United States United States
No Biography provided

Comments and Discussions

 
GeneralCould you reflector an dll or exe file which have a strong key name Pinmemberlpbinh21-Jul-05 16:32 
GeneralConfusing title PinmemberAndre Seibel21-Oct-04 19:03 
GeneralRe: Confusing title Pinmemberzaccheus25-Feb-07 0:35 
Questionnamespaces? PinmemberStanimir_Stoyanov15-Feb-04 21:19 
GeneralCode in pre tags PinmemberJonathan de Halleux12-Jan-04 5:39 
GeneralRe: Code in pre tags PinsitebuilderUwe Keim12-Jan-04 6:22 
GeneralRe: Code in pre tags Pinmemberdog_spawn12-Jan-04 7:36 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150327.1 | Last Updated 12 Jan 2004
Article Copyright 2004 by Jayakrishna Damarla
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid