internal Type FindOrCreateType(Type interfaceType)
{
if (!interfaceType.IsInterface)
{
throw Fx.AssertAndThrow("Passed in type should be an Interface");
}
Type classType = null;
lock (this)
{
interfaceToClassMap.TryGetValue(interfaceType, out classType);
if (classType == null)
{
TypeBuilder typeBuilder = DynamicModule.DefineType(interfaceType.Name + "MarshalByRefObject", TypeAttributes.Public | TypeAttributes.Class | TypeAttributes.Abstract,
typeof(MarshalByRefObject), new Type[] { interfaceType });
Type[] ctorParams = new Type[] { typeof(ClassInterfaceType) };
ConstructorInfo classCtorInfo = typeof(ClassInterfaceAttribute).GetConstructor(ctorParams);
CustomAttributeBuilder attributeBuilder = new CustomAttributeBuilder(classCtorInfo,
new object[] { ClassInterfaceType.None });
typeBuilder.SetCustomAttribute(attributeBuilder);
typeBuilder.AddInterfaceImplementation(interfaceType);
foreach (MethodInfo mInfo in interfaceType.GetMethods())
{
MethodBuilder methodInClass = null;
methodInClass = typeBuilder.DefineMethod(mInfo.Name,
MethodAttributes.Public | MethodAttributes.Virtual |
MethodAttributes.Abstract | MethodAttributes.Abstract | MethodAttributes.HideBySig | MethodAttributes.NewSlot,
mInfo.ReturnType, GetParameterTypes(mInfo));
}
classType = typeBuilder.CreateType();
interfaceToClassMap[interfaceType] = classType;
}
}
if (classType == null)
{
throw Fx.AssertAndThrow("Class Type should not be null at this point");
}
return classType;
}
}