Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / CLR / September 2005

Tip: Looking for answers? Try searching our database.

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

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Corey Kosak - 18 Sep 2005 01:11 GMT
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 - 18 Sep 2005 12:30 GMT
Corey,

>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...

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

Mattias

Signature

Mattias Sjögren [MVP]  mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

Corey Kosak - 18 Sep 2005 14:49 GMT
> 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?
Robert Jordan - 18 Sep 2005 16:27 GMT
Hi,

>>Since TypeBuilder derives from Type, you can try using typeBuilder
>>there.
[quoted text clipped - 7 lines]
> 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
Corey Kosak - 18 Sep 2005 17:58 GMT
Yee-haw!  Works great.  Thank you!

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

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.