473,385 Members | 1,813 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Creating interface type at run-time using code emitting

I need to create interface "on-the-fly" so it "reflects" some class (mainly it's properties). Here's how I do this:

Expand|Select|Wrap|Line Numbers
  1.     public class Transformer
  2.     {
  3.         private const TypeAttributes INTERFACE_TYPE_ATTRIBUTES =
  4.             TypeAttributes.Public |
  5.             TypeAttributes.Interface |
  6.             TypeAttributes.Abstract |
  7.             TypeAttributes.AutoClass |
  8.             TypeAttributes.AnsiClass;
  9.  
  10.         private const BindingFlags GENERAL_FLAGS =
  11.             BindingFlags.Public |
  12.             BindingFlags.Instance;
  13.  
  14.         AssemblyBuilder _assemblyBuilder = null;
  15.         ModuleBuilder _moduleBuilder = null;
  16.         Type _mockeryType = null;
  17.         Type _bakedType = null;
  18.  
  19.         public Transformer(Type mockeryType)
  20.         {
  21.             _mockeryType=mockeryType;
  22.             AssemblyName assemblyName = new AssemblyName("DynamicModule");
  23.             _assemblyBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.RunAndSave);
  24.             _moduleBuilder = _assemblyBuilder.DefineDynamicModule("DynamicMyClass.Module", "DynamicModule.dll", false);
  25.             _bakedType = CreateInterfaceType();
  26.             _assemblyBuilder.Save("DynamicModule.dll");
  27.         }
  28.  
  29.         private static Type[] GetParameterTypes(ParameterInfo[] paramInfos)
  30.         {
  31.             Type[] types = new Type[paramInfos.Length];
  32.             for (int i = 0; i < paramInfos.Length; ++i)
  33.             {
  34.                 types[i] = paramInfos[i].ParameterType;
  35.             }
  36.  
  37.             return types;
  38.         }
  39.  
  40.         private Type CreateInterfaceType()
  41.         {
  42.             TypeBuilder interfaceTypeBuilder =
  43.                 _moduleBuilder.DefineType("ID" + _mockeryType.Name, INTERFACE_TYPE_ATTRIBUTES, null);
  44.  
  45.             Converter<MethodInfo, MethodBuilder> defineInterfaceMethod = delegate(MethodInfo methodInfo)
  46.                                                                     {
  47.                                                                         return interfaceTypeBuilder.DefineMethod(
  48.                                                                             methodInfo.Name,
  49.                                                                             MethodAttributes.Public |
  50.                                                                             MethodAttributes.HideBySig |
  51.                                                                             MethodAttributes.SpecialName |
  52.                                                                             MethodAttributes.NewSlot |
  53.                                                                             MethodAttributes.Abstract |
  54.                                                                             MethodAttributes.Virtual,
  55.                                                                             CallingConventions.HasThis,
  56.                                                                             methodInfo.ReturnType,
  57.                                                                             GetParameterTypes(methodInfo.GetParameters())
  58.                                                                             );
  59.                                                                     };
  60.  
  61.             PropertyInfo[] classProps = _mockeryType.GetProperties(GENERAL_FLAGS);
  62.             foreach (PropertyInfo propertyInfo in classProps)
  63.             {
  64.                 PropertyBuilder propertyBuilder = interfaceTypeBuilder.DefineProperty(
  65.                     propertyInfo.Name,
  66.                     propertyInfo.Attributes,
  67.                     propertyInfo.PropertyType,
  68.                     null,
  69.                     null,
  70.                     GetParameterTypes(propertyInfo.GetIndexParameters()),
  71.                     null,
  72.                     null);
  73.                 if (propertyInfo.CanRead)
  74.                 {
  75.                     propertyBuilder.SetGetMethod(
  76.                         defineInterfaceMethod(propertyInfo.GetGetMethod(true))
  77.                         );
  78.                 }
  79.  
  80.                 if (propertyInfo.CanWrite)
  81.                 {
  82.                     propertyBuilder.SetSetMethod(
  83.                         defineInterfaceMethod(propertyInfo.GetSetMethod(true))
  84.                         );
  85.                 }
  86.             }
  87.  
  88.             return interfaceTypeBuilder.CreateType();
  89.         }
  90.  
  91.         private MethodBuilder DefineMethodInt(TypeBuilder interfaceTypeBuilder, MethodInfo methodInfo)
  92.         {
  93.              ParameterInfo[] pi = methodInfo.GetParameters();
  94.             Type[] tps = new Type[pi.Length];
  95.             for (int i = 0; i < tps.Length; i++)
  96.             {
  97.                 tps[i] = pi[i].ParameterType;
  98.             }
  99.             MethodBuilder methodBuilder = interfaceTypeBuilder.DefineMethod(methodInfo.Name,
  100.                 MethodAttributes.Public |
  101.                 MethodAttributes.HideBySig |
  102.                 MethodAttributes.SpecialName |
  103.                 MethodAttributes.NewSlot |
  104.                 MethodAttributes.Abstract |
  105.                 MethodAttributes.Virtual,
  106.             methodInfo.ReturnType, tps);
  107.             return methodBuilder;
  108.         }
  109.     }
  110.  
The goal is, having a class like:
Expand|Select|Wrap|Line Numbers
  1.     public class TransformTest
  2.     {
  3.         private string _SF;
  4.         public string SF
  5.         {
  6.             get { return _SF; }
  7.             set { _SF = value; }
  8.         }
  9.     };
  10.  
to get interface that looks like:
Expand|Select|Wrap|Line Numbers
  1.     public interface IDTransformTest
  2.     {
  3.         string SF{get;set;}
  4.     }
  5.  
Ok... Using the code above it seems like I built it... but (!!!) looking at IL representation of the newly generated type (using Reflector) here's what I see:

Expand|Select|Wrap|Line Numbers
  1. .class public interface abstract auto ansi IDTransformTest
  2. {
  3.     .property class string SF
  4.     {
  5.         .get instance string ConsoleApplication4.ITransformTest::get_SF()
  6.         .set instance void ConsoleApplication4.ITransformTest::set_SF(string)
  7.     }
  8. }
  9.  
while it should looks like this:
Expand|Select|Wrap|Line Numbers
  1. .class public interface abstract auto ansi ITransformTest
  2. {
  3.     .property instance string SF
  4.     {
  5.         .get instance string ConsoleApplication4.ITransformTest::get_SF()
  6.         .set instance void ConsoleApplication4.ITransformTest::set_SF(string)
  7.     }
  8. }
  9.  
The difference is in parameter standing right after .property qualifier:
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.
May 21 '07 #1
0 1251

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Hal Vaughan | last post by:
I'm creating a sequence of JPanels that will each, in turn, be added to (then removed from) a window (kind of like a wizard). I want these panels to not only extend the JPanel class, but to...
6
by: Chamith | last post by:
Hi all, I have a method which takes a string parameter. It takes a type name as a string. Inside the method, i want to create an instance of the type which is mentioned in the string parameter. ...
2
by: Pawan | last post by:
Hi Guys, I have this current assignment where I have to develop online forms for local municipal authorities. I have to use adobe acrobat to create online forms from PDFs (which I have never done...
7
by: yufufi | last post by:
lets say we have a 'shape' class which doesn't implement IComparable interface.. compiler doesn't give you error for the lines below.. shape b= new shape(); IComparable h; h=(IComparable)b;...
2
by: Dan | last post by:
I have need to create a com server that will fire events back to clients. I have looked at some of the documentation and I am new to Python so cant quite get it. I have created an IDL file of the...
31
by: JoeC | last post by:
I have read books and have ideas on how to create objects. I often create my own projects and programs. They end up getting pretty complex and long. I often use objects in my programs they are...
3
by: psbasha | last post by:
Hi, I would like to call the same aplication executable with and without Graphical User Interface. Requirement: With Tkinter User interface,user can give the inputs to run the application (...
6
by: Chris Carlen | last post by:
Hi: I have an embedded system, platform: TI TMS320F2812 32-bit integer DSP. A module (.c file) contains external (to the funcs in that module) variables which are used to affect the operation...
12
by: ross m. greenberg | last post by:
I'm trying to create a function in JavaScript using Dreamweaver8 such that when a user hits the ' F1' key when a text box is selected a separate "pop-up" window will open, with text of my choice. ...
7
by: Jwe | last post by:
Hi, I've written a program which has both a command line interface and Windows form interface, however it isn't quite working correctly. When run from command line with no arguments it should...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.