Search for ArgumentNullException
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:
- Iterates over the types in the assembly. IAssembly.GetAllTypes returns a collection of the assembly’s types, which are represented by
- Iterates over the methods in each type. INamedTypeDefinition.Methods contains a collection of the type’s methods, which are represented by
- Iterates over the operations in each method body. IMethodDefinition.Body.Operations contains a collection of the method’s operations, represented by
FindThrowPlatformType looks for the Operation.Newobj
operation code, which corresponds to new object construction. For details, see
. 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
To determine whether the application is creating a new ArgumentNullException
- 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
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