Find Generic Methods

CCI Metadata

The metadata for each type in an assembly is represented by a NamedTypeDefinition object. The object contains a collection of MethodDefinition objects that represent the metadata for each of the type’s methods. The MethodDefinition and NamedTypeDefinition objects include an IsGeneric property that indicates whether the type or method is generic. Because FindGeneric works with an immutable representation of an assembly, the objects are represented by their immutable interfaces, INamedTypeDefinition and IMethodDefinition.

The following example shows how FindGeneric searches an assembly for generic methods. The basic procedure can be easily adapted to search for other types of information.
foreach (INamedTypeDefinition type in assembly.GetAllTypes())
{
  if (!type.IsGeneric) continue;
  foreach (IMethodDefinition methodDefinition in type.Methods)
  {
    if (methodDefinition.IsGeneric)
    {
      Console.WriteLine(MemberHelper.GetMemberSignature(methodDefinition,
        NameFormattingOptions.Signature | NameFormattingOptions.TypeParameters | NameFormattingOptions.TypeConstraints | NameFormattingOptions.ParameterName));
    }
  }
}

FindGeneric examines each type in the assembly. If the type is generic, FindGeneric examines the type’s MethodDefinition objects. For each generic method, FindGeneric uses the MemberHelper.GetMemberSignature method to print the signature in the command window.

Next: Add a Test Class to FindGeneric
Return to Beginning

Last edited Jan 12, 2010 at 4:31 PM by Guy_Smith, version 9

Comments

No comments yet.