Search for ArgumentNullException

CCI Metadata

FindThrowPlatformType searches the assembly for new instances of ArgumentNullException, as shown in the following example.
foreach (INamedTypeDefinition type in assembly.GetAllTypes())
  foreach (IMethodDefinition methodDefinition in type.Methods)
    foreach (IOperation operation in methodDefinition.Body.Operations)
      if (operation.OperationCode == OperationCode.Newobj)
        IMethodReference consRef = operation.Value as IMethodReference;
        if (consRef != null && consRef.Name.Value.Equals(".ctor") &&
            consRef.ContainingType.InternedKey == systemArgumentNullException.InternedKey)

To search the assembly, FindThrowPlatformType:
  1. Iterates over the types in the assembly. IAssembly.GetAllTypes returns a collection of the assembly’s types, which are represented by INamedTypeDefinition interfaces.
  2. Iterates over the methods in each type. INamedTypeDefinition.Methods contains a collection of the type’s methods, which are represented by IMethodDefinition interfaces.
  3. Iterates over the operations in each method body. IMethodDefinition.Body.Operations contains a collection of the method’s operations, represented by IOperation interfaces.
FindThrowPlatformType looks for the Operation.Newobj operation code, which corresponds to new object construction. For details, see OpCodes.Newobj Field. The value associated with Newobj is a metadata token that indicates the name, class and signature of the constructor to call. CCI Metadata represents this token with an IMethodReference interface.

To determine whether the application is creating a new ArgumentNullException object, FindThrowPlatformType:
  • Verifies that the referenced method is a constructor. IMethodReference.Name contains the method name, represented by an IName interface. IName.Value contains the name itself, which should be “.ctor”. For more information on IName, see The IName Interface.
  • Determines whether the type is ArgumentNullException. The host’s InternFactory. object assigns a unique InternedKey value to each type that is used by the application. You can determine whether two types are identical by comparing the keys. The NamespaceTypeReference.InternedKey property from the ArgumentNullException type reference object that was created earlier contains the key for ArgumentNullException. The MethodReference.ContainingType.InternedKey contains the key for the type being created.
If the keys are equal, the newly created object is ArgumentNullException and FindThrowPlatformType prints the method signature.

Next: Add a Test Class to FindThrowPlatformType
Return to Beginning

Last edited Jan 15, 2010 at 11:05 PM by Guy_Smith, version 3


No comments yet.