0

I am working on a solution that extracts all the class associations between each other. If we click a class in the object browser we can see the option Find All References. I want something similar that finds the associations a class has to other classes. How can we find them using Reflection?

I am interested in Extracting

  1. Composition Relationship:
  2. Aggregation Relationship
  3. Association Relationship
  4. Inheritance
Olivier Jacot-Descombes
  • 104,806
  • 13
  • 138
  • 188
Afnan Bashir
  • 7,319
  • 20
  • 76
  • 138
  • Reflection in .NET only inspects metadata, not IL. You would have to use something like Mono.Cecil to inspect a class and its members to see which types it is using. – vcsjones Apr 30 '12 at 14:58
  • IL means ? and have any idea ab http://www.codeproject.com/Articles/17823/100-Reflective-Class-Diagram-Creation-Tool – Afnan Bashir Apr 30 '12 at 15:22

2 Answers2

1

You can use Reflection in order to list all properties, fields and methods (and more) of a class. You would have to inspect these and determine their types and parameter lists. Probably it would be a good idea to discard types from the System namespace.

Type t = typeof(MyClass);

// Determine from which type a class inherits
Type baseType = t.BaseType;

// Determine which interfaces a class implements
Type[] interfaces = t.GetInterfaces();

// Get fields, properties and methods
const BindingFlags bindingFlags =
    BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic;
FieldInfo[] fields = t.GetFields(bindingFlags);
PropertyInfo[] properties = t.GetProperties(bindingFlags);
MethodInfo[] methods = t.GetMethods(bindingFlags);

foreach (var method in methods) {
    // Get method parameters
    ParameterInfo[] parameters = method.GetParameters();
}

Intellisense will tell you (almost) all the secrets of FieldInfo, PropertyInfo, MethodInfo and ParameterInfo. You might also consider examining the generic parameters of the class, events and more.

Olivier Jacot-Descombes
  • 104,806
  • 13
  • 138
  • 188
  • This would only retrieve types exposed publicly; you will be able to answer "what types are exposed by X", not "what types are used by X". – sisve Apr 30 '12 at 18:08
  • The `BindingFlags.NonPublic` will show you non-public members as well; however, local variables and types used locally in some way cannot be listed with reflection, but those won't be part of composition, aggregation, association and inheritance anyway. – Olivier Jacot-Descombes May 01 '12 at 13:04
1

You would need to parse the generated IL code, preferable with an understanding how they work. You can retrieve the IL code by calling MethodBase.GetMethodBody followed by MethodBody.GetILAsByteArray.

You need to parse every byte according to the OpCodes enum. You'll also need to handle all opcode data, like offsets and tokens, since you shouldn't parse them as actual opcodes. Take the OpCodes.Ldstr opcode for example; it consists of 0x72 followed by a token. This token is four bytes that should be parsed as an Int32, and can be resolved using Module.ResolveString (since you know that ldstr has a string token). You can get a Module instance from Assembly.AssemblyManifestModule.

sisve
  • 19,501
  • 3
  • 53
  • 95