Click here to Skip to main content
Licence 
First Posted 11 Jan 2004
Views 41,940
Bookmarked 22 times

Reflector

By | 11 Jan 2004 | Article
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

About the Author

Jayakrishna Damarla

Web Developer

United States United States

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralCould you reflector an dll or exe file which have a strong key name Pinmemberlpbinh15:32 21 Jul '05  
GeneralConfusing title PinmemberAndre Seibel18:03 21 Oct '04  
GeneralRe: Confusing title Pinmemberzaccheus23:35 24 Feb '07  
Questionnamespaces? PinmemberStanimir_Stoyanov20:19 15 Feb '04  
GeneralCode in pre tags PinmemberJonathan de Halleux4:39 12 Jan '04  
GeneralRe: Code in pre tags PinsitebuilderUwe Keim5:22 12 Jan '04  
GeneralRe: Code in pre tags Pinmemberdog_spawn6:36 12 Jan '04  

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.

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