Expand|Select|Wrap|Line Numbers
- public class Transformer
- {
- private const TypeAttributes INTERFACE_TYPE_ATTRIBUTES =
- TypeAttributes.Public |
- TypeAttributes.Interface |
- TypeAttributes.Abstract |
- TypeAttributes.AutoClass |
- TypeAttributes.AnsiClass;
- private const BindingFlags GENERAL_FLAGS =
- BindingFlags.Public |
- BindingFlags.Instance;
- AssemblyBuilder _assemblyBuilder = null;
- ModuleBuilder _moduleBuilder = null;
- Type _mockeryType = null;
- Type _bakedType = null;
- public Transformer(Type mockeryType)
- {
- _mockeryType=mockeryType;
- AssemblyName assemblyName = new AssemblyName("DynamicModule");
- _assemblyBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.RunAndSave);
- _moduleBuilder = _assemblyBuilder.DefineDynamicModule("DynamicMyClass.Module", "DynamicModule.dll", false);
- _bakedType = CreateInterfaceType();
- _assemblyBuilder.Save("DynamicModule.dll");
- }
- private static Type[] GetParameterTypes(ParameterInfo[] paramInfos)
- {
- Type[] types = new Type[paramInfos.Length];
- for (int i = 0; i < paramInfos.Length; ++i)
- {
- types[i] = paramInfos[i].ParameterType;
- }
- return types;
- }
- private Type CreateInterfaceType()
- {
- TypeBuilder interfaceTypeBuilder =
- _moduleBuilder.DefineType("ID" + _mockeryType.Name, INTERFACE_TYPE_ATTRIBUTES, null);
- Converter<MethodInfo, MethodBuilder> defineInterfaceMethod = delegate(MethodInfo methodInfo)
- {
- return interfaceTypeBuilder.DefineMethod(
- methodInfo.Name,
- MethodAttributes.Public |
- MethodAttributes.HideBySig |
- MethodAttributes.SpecialName |
- MethodAttributes.NewSlot |
- MethodAttributes.Abstract |
- MethodAttributes.Virtual,
- CallingConventions.HasThis,
- methodInfo.ReturnType,
- GetParameterTypes(methodInfo.GetParameters())
- );
- };
- PropertyInfo[] classProps = _mockeryType.GetProperties(GENERAL_FLAGS);
- foreach (PropertyInfo propertyInfo in classProps)
- {
- PropertyBuilder propertyBuilder = interfaceTypeBuilder.DefineProperty(
- propertyInfo.Name,
- propertyInfo.Attributes,
- propertyInfo.PropertyType,
- null,
- null,
- GetParameterTypes(propertyInfo.GetIndexParameters()),
- null,
- null);
- if (propertyInfo.CanRead)
- {
- propertyBuilder.SetGetMethod(
- defineInterfaceMethod(propertyInfo.GetGetMethod(true))
- );
- }
- if (propertyInfo.CanWrite)
- {
- propertyBuilder.SetSetMethod(
- defineInterfaceMethod(propertyInfo.GetSetMethod(true))
- );
- }
- }
- return interfaceTypeBuilder.CreateType();
- }
- private MethodBuilder DefineMethodInt(TypeBuilder interfaceTypeBuilder, MethodInfo methodInfo)
- {
- ParameterInfo[] pi = methodInfo.GetParameters();
- Type[] tps = new Type[pi.Length];
- for (int i = 0; i < tps.Length; i++)
- {
- tps[i] = pi[i].ParameterType;
- }
- MethodBuilder methodBuilder = interfaceTypeBuilder.DefineMethod(methodInfo.Name,
- MethodAttributes.Public |
- MethodAttributes.HideBySig |
- MethodAttributes.SpecialName |
- MethodAttributes.NewSlot |
- MethodAttributes.Abstract |
- MethodAttributes.Virtual,
- methodInfo.ReturnType, tps);
- return methodBuilder;
- }
- }
Expand|Select|Wrap|Line Numbers
- public class TransformTest
- {
- private string _SF;
- public string SF
- {
- get { return _SF; }
- set { _SF = value; }
- }
- };
Expand|Select|Wrap|Line Numbers
- public interface IDTransformTest
- {
- string SF{get;set;}
- }
Expand|Select|Wrap|Line Numbers
- .class public interface abstract auto ansi IDTransformTest
- {
- .property class string SF
- {
- .get instance string ConsoleApplication4.ITransformTest::get_SF()
- .set instance void ConsoleApplication4.ITransformTest::set_SF(string)
- }
- }
Expand|Select|Wrap|Line Numbers
- .class public interface abstract auto ansi ITransformTest
- {
- .property instance string SF
- {
- .get instance string ConsoleApplication4.ITransformTest::get_SF()
- .set instance void ConsoleApplication4.ITransformTest::set_SF(string)
- }
- }
class in my dynamic interface type
instance in the "real" interface type
What is wrong with my approach and how to correct it?
Thanks in advance for any help.