HighTechTalks DotNet Forums  

How to dynamically gen class A containing a method that returns A[

Dotnet Framework (CLR) microsoft.public.dotnet.framework.clr


Discuss How to dynamically gen class A containing a method that returns A[ in the Dotnet Framework (CLR) forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old   
Corey Kosak
 
Posts: n/a

Default How to dynamically gen class A containing a method that returns A[ - 09-17-2005 , 08:11 PM






I am trying to dynamically generate a class like the following:

public class A {
public static A[] Func(ref A a) {
...
}
}

The point is that the function has an argument type and a return type that
are composed from the same type that is being built (in this case I need a
managed-pointer-to-A and array-of-A). My problem is I don't know how to get
a Type object for these types, when all I have is a TypeBuilder that I'm not
even done building yet. In other words:

AssemblyName assemblyName = new AssemblyName();
assemblyName.Name = "MyDynamicAssembly";

AssemblyBuilder assemblyBuilder =
AppDomain.CurrentDomain.DefineDynamicAssembly(asse mblyName,
AssemblyBuilderAccess.Run);
ModuleBuilder moduleBuilder =
assemblyBuilder.DefineDynamicModule("MyDynamicModu le");
TypeBuilder typeBuilder = moduleBuilder.DefineType("MyDynamicType",
TypeAttributes.Public);

MethodBuilder mb=typeBuilder.DefineMethod("Func",
MethodAttributes.Public|MethodAttributes.Static,
XXX, new Type[]{YYY});


What can I use for XXX and YYY here? I'm sure I'm missing something, but
I've been banging my head on this for a while now...


Reply With Quote
  #2  
Old   
Corey Kosak
 
Posts: n/a

Default Re: How to dynamically gen class A containing a method that return - 09-18-2005 , 09:49 AM






"Mattias Sjögren" wrote:
Quote:
Since TypeBuilder derives from Type, you can try using typeBuilder
there.
But if I simply use the TypeBuilder I have, I will end up defining a
function looking like this:

public static A Func(A a) {...}

which is not what I need. How do I, starting from the TypeBuilder for A
that I have, get to a TypeBuilder for array-of-A and managed-pointer-to-A?
Can you give an example?



Reply With Quote
  #3  
Old   
Robert Jordan
 
Posts: n/a

Default Re: How to dynamically gen class A containing a method that return - 09-18-2005 , 11:27 AM



Hi,

Quote:
"Mattias Sjögren" wrote:

Since TypeBuilder derives from Type, you can try using typeBuilder
there.


But if I simply use the TypeBuilder I have, I will end up defining a
function looking like this:

public static A Func(A a) {...}

which is not what I need. How do I, starting from the TypeBuilder for A
that I have, get to a TypeBuilder for array-of-A and managed-pointer-to-A?
Can you give an example?
You need the get the incomplete types from the ModuleBuilder:

Type arrayType = moduleBuilder.GetType ("A[]");
Type refType = moduleBuilder.GetType ("A&");

MethodBuilder mb = typeBuilder.DefineMethod("Func",
MethodAttributes.Public|MethodAttributes.Static,
arrayType, new Type[]{refType});


Rob


Reply With Quote
  #4  
Old   
Corey Kosak
 
Posts: n/a

Default Re: How to dynamically gen class A containing a method that return - 09-18-2005 , 12:58 PM



Yee-haw! Works great. Thank you!


"Robert Jordan" wrote:
Quote:
You need the get the incomplete types from the ModuleBuilder:
Type arrayType = moduleBuilder.GetType ("A[]");
Type refType = moduleBuilder.GetType ("A&");


Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.