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 / Languages / C# / March 2008

Tip: Looking for answers? Try searching our database.

Creating struct instead of class using ModuleBuilder.DefineType

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Marek - 13 Mar 2008 13:38 GMT
Hi
I am trying to dynamically create a struct using the following code:

TypeBuilder typeBuilder = moduleBuilder.DefineType("MyStruct",
TypeAttributes.Public);

It appears that this method will only ever create classes for me.  Is there
any way of dynamically creating a struct?  I noticed that other constructs
such as enums have their own equivalent define method (DefineEnum), but
couldn't see a way of doing this for structs.

I have to use a struct as this object is passed to a FORTRAN DLL which does
not like the class equivalent.

Best regards

Marek
Marc Gravell - 13 Mar 2008 14:07 GMT
Have you tried using typeof(ValueType) as the parent? At the IL level,
both struct and class are IL classes - i.e. here is the sig from Int32
(IL view in reflector):

.class public sequential ansi serializable sealed beforefieldinit
Int32
   extends System.ValueType
   implements System.IComparable, System.IFormattable,
System.IConvertible, System.IComparable`1<int32>,
System.IEquatable`1<int32>

That seems to work - at least in a quick test CreateType().IsValueType
returned true.

Of course, you still might have some fun getting this new Type into
your external API...

Marc
Marc Gravell - 13 Mar 2008 14:08 GMT
(example)

       AssemblyName an = new AssemblyName("Foo");
       AssemblyBuilder ab =
AppDomain.CurrentDomain.DefineDynamicAssembly(an,
AssemblyBuilderAccess.Run);
       ModuleBuilder mb = ab.DefineDynamicModule("Foo");
       TypeBuilder tb = mb.DefineType("Foo", TypeAttributes.Public |
TypeAttributes.Sealed | TypeAttributes.SequentialLayout |
TypeAttributes.Serializable, typeof(ValueType));
       Type type = tb.CreateType();
       bool isVal = type.IsValueType;
Marek - 13 Mar 2008 14:22 GMT
Hi Marc
You're a star.  That worked a treat thanks.

Nice use of the parent type parameter - I looked at it but didn't think of
using it in this way.

Best regards

Marek

> (example)
>
[quoted text clipped - 8 lines]
>         Type type = tb.CreateType();
>         bool isVal = type.IsValueType;

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.