Groups | Blog | Home
all groups > dotnet clr > september 2005 >

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


Corey Kosak
9/17/2005 5:11:01 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(assemblyName,
AssemblyBuilderAccess.Run);
ModuleBuilder moduleBuilder =
assemblyBuilder.DefineDynamicModule("MyDynamicModule");
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...
Mattias Sjögren
9/18/2005 12:00:00 AM
Corey,

[quoted text, click to view]

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


Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Robert Jordan
9/18/2005 12:00:00 AM
Hi,

[quoted text, click to view]

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});


Corey Kosak
9/18/2005 6:49:01 AM
[quoted text, click to view]

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?
Corey Kosak
9/18/2005 9:58:01 AM
Yee-haw! Works great. Thank you!


[quoted text, click to view]
AddThis Social Bookmark Button