This project is read-only.

Create a Type Reference for ArgumentNullException

CCI Metadata

Before an application can throw an exception, it must create an appropriate exception object. FindThrowPlatformType determines whether an assembly throws ArgumentNullException by looking for the object’s constructor. ArgumentNullException is in the .NET core assembly, mscorlib.dll. To identify a constructor from an external assembly, you must compare type references.

Type references are represented in CCI Metadata by NamespaceTypeReference objects, or by the INamespaceTypeReference interface. The interface is an immutable representation of the type reference, and provides read-only access to the underlying NamespaceTypeReference object’s properties. For more discussion, see Mutable and Immutable Representations.

FindThrowPlatformType creates a type reference for System.ArgumentNullException as follows.
MyPlatformType platformType = new MyPlatformType(host);
INamespaceTypeReference systemArgumentNullException = platformType.SystemArgumentNullException;

The mechanics of creating the ArgumentNullException reference are handled by an internal class, MyPlatformType. This class inherits from Microsoft.Cci.PlatformType, and includes a SystemArgumentNullException property which contains the type reference.
internal class MyPlatformType : PlatformType
  INamespaceTypeReference systemArgumentNullException;
  public INamespaceTypeReference SystemArgumentNullException
      if (this.systemArgumentNullException == null)
        this.systemArgumentNullException = this.CreateReference(
          this.CoreAssemblyRef, "System", "ArgumentNullException");
      return this.systemArgumentNullException;

The two key elements of this example are the CreateReference method and the CoreAssemblyRef property, both of which are implemented by the base PlatformType type.

Most systems include multiple versions of mscorlib.dll, so you must ensure that you are getting a reference to the correct version. The host is responsible for determining the appropriate mscorlib.dll version. DefaultHost specifies unification for mscorlib.dll, which directs .NET to use the most recent version. DefaultHost stores a reference to that assembly in its CoreAssemblyRef property.

CreateReference creates a NamespaceTypeReference object to represent the type reference and returns INamespaceTypeReference interface on the object. The method takes three parameters, which identify the type:
  1. A reference to the DLL that hosts the type. In this case, it’s a reference to the mscorlib.dll version from DefaultHost.
  2. The type name as an ordered list of strings, one for each element in the fully-qualified name. In this case, the fully-qualified name is System.ArgumentNullException, so the list consists of a single namespace followed by the type name.

Next: Search for ArgumentNullException
Return to Beginning

Last edited Jan 15, 2010 at 4:29 PM by Guy_Smith, version 1


No comments yet.