Writing custom pipeline components
The Microsoft.CCF.Common.TypeProvider.dll assembly provides various interfaces that you can use to customize each of the pipeline stages as discussed in the previous section. You can implement the interfaces in independent objects or as a single object.
You can use the ITypeResolver interface to customize the resolution stage. You can use two methods to resolve the parsed name:
- ResolveTypeName(): Resolves the name in a given context.
- ResolveAllTypeNames(): Returns a list of fully qualified names for all matching types.
You must pass the context objects for both the methods, else the methods will take null context objects. The following code shows an example of the context object:
public class CustomTypeResolver: ITypeResolver
{
#region ITypeResolver Members
public string ResolveTypeName(string typeName, object typeContext)
{
// return the fully qualified name of the type matching the name in the given context
}
public IEnumerable<string> ResolveAllTypeNames(string baseTypeName, object typeContext)
{
//return the list of fully qualified names of types matching the name in the given context
}
#endregion
}
You can use the ITypeLoader interface to customize the Loading stage of the pipeline. The interface requires the implementation of two methods:
- GetAssembly(): You can use the method to retrive embedded resources while using the TypeProvider.
- GetType(): You can use the method to retrieve embedded resource if you are not using the TypeProvider.
You can use a custom type retrival where a standard type is not used. In this case, you can call a .Net subsystem (as in Type.GetType()) before the custom code.
The following code shows a sample implementation of the method:
public class CustomTypeLoader : ITypeLoader
{
#region ITypeLoader Members
public System.Reflection.Assembly GetAssembly(string assemblyName)
{
// return here the assembly mapping to the particular name
}
public Type GetType(string typeName)
{
// return here the loaded type corresponding to the fullyQualified name passed
}
#endregion
}
The ITypeActivator interface is the customization of the last component of the TypeProvider pipeline. You can use the CreateInstance() method in two different ways. They two ways differ in the ability to pass constructors and parameters, when instantiating the object.
public class CustomTypeActivator : ITypeActivator
{
#region ITypeActivator Members
public object CreateInstance(Type type)
{
// return here the object intantiated from the provided Type
}
public object CreateInstance(Type type, object[] args)
{
// return the object intantiated from the provided Type using the constructor’s parameters provided in args
}
#endregion
}