473,406 Members | 2,439 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,406 software developers and data experts.

use an enum in an emitted assembly


I want create a method which uses a dynamically enum as the type of one
of its parameters.

I can create the assembly with this code:

AssemblyName myAssemblyName = new AssemblyName();
myAssemblyName.Name = "EmittedAssembly";

// Create the dynamic assembly.
myAssemblyBuilder = myAppDomain.DefineDynamicAssembly(myAssemblyName,
AssemblyBuilderAccess.Save);

// Create a dynamic module.
myModuleBuilder =
myAssemblyBuilder.DefineDynamicModule("EmittedModu le",
"EmittedModule.mod");

// Create a dynamic Enum.
myEnumBuilder = myModuleBuilder.DefineEnum("MyNamespace.MyEnum",
TypeAttributes.Public, typeof(Int32));

FieldBuilder myFieldBuilder1
= myEnumBuilder.DefineLiteral("FieldOne", 1);

FieldBuilder myFieldBuilder2
= myEnumBuilder.DefineLiteral("FieldTwo", 2);

myEnumBuilder.CreateType();
myAssemblyBuilder.Save("Ass1.dll");

But, how do I access this in a method.

I want to do something like

class blah {

myMethod( NyNamespace.MyEnum e)
{
...
}}

But it will not let me use this or

void method(a.GetType("MyNamespace.MyEnum") b)
{
}
How can I use an enum in an emitted assembly to define the input
parameter of a method?
Jul 21 '05 #1
5 2061
Hi Moe,

The problem with what you are trying to accomplish is when each part of the
code is defined. The method, and its parameters, are defined at compile
time. However, your enum is defined at run time. The only way to get the
behavior you want is to dynamically build your method at runtime also.

Joe
--
http://www.csharp-station.com

"Moe Green" <mo*@green.vegas> wrote in message
news:33*************@individual.net...

I want create a method which uses a dynamically enum as the type of one of
its parameters.

I can create the assembly with this code:

AssemblyName myAssemblyName = new AssemblyName();
myAssemblyName.Name = "EmittedAssembly";

// Create the dynamic assembly.
myAssemblyBuilder = myAppDomain.DefineDynamicAssembly(myAssemblyName,
AssemblyBuilderAccess.Save);

// Create a dynamic module.
myModuleBuilder =
myAssemblyBuilder.DefineDynamicModule("EmittedModu le",
"EmittedModule.mod");

// Create a dynamic Enum.
myEnumBuilder = myModuleBuilder.DefineEnum("MyNamespace.MyEnum",
TypeAttributes.Public, typeof(Int32));

FieldBuilder myFieldBuilder1
= myEnumBuilder.DefineLiteral("FieldOne", 1);

FieldBuilder myFieldBuilder2
= myEnumBuilder.DefineLiteral("FieldTwo", 2);

myEnumBuilder.CreateType();
myAssemblyBuilder.Save("Ass1.dll");

But, how do I access this in a method.

I want to do something like

class blah {

myMethod( NyNamespace.MyEnum e)
{
...
}}

But it will not let me use this or

void method(a.GetType("MyNamespace.MyEnum") b)
{
}
How can I use an enum in an emitted assembly to define the input parameter
of a method?

Jul 21 '05 #2
Joe Mayo wrote:
Hi Moe,

The problem with what you are trying to accomplish is when each part of the
code is defined. The method, and its parameters, are defined at compile
time. However, your enum is defined at run time. The only way to get the
behavior you want is to dynamically build your method at runtime also.

Joe


Thanks, Joe.

I've seen code showing how to create the Enum dynamically at run time.

But when it comes time to utilize that as the input type of an actual method

void myMethod ( myDynamicEnum mde) { .... }

I can't seem to make heads of tails of it !

For example, I can globally declare the Assembly at the class level, but
if I then try and define a Type using the GetType method of Assembly,
Visual Studio barfs on me.

Can it be done? It seems doable...but I'm just missing some key trick...
Jul 21 '05 #3
Hi Steve,

Here are a few tips:

1. When you instantiate your AssemblyBuilder, you need to change its access
from AssemblyBuilderAccess.Save to AssemblyBuilderAccess.RunAndSave.

2. Get an instance of your enum via the Enum.ToObject() method.

3. Write the code for the method you want to dynamically generate. Then
use ILDASM to see the IL it generates. Use the compiler generated IL to
figure out what opcodes to use in the ILGenerator.Emit calls.

Here's an example of what you need to do:

using System;
using System.Reflection;
using System.Reflection.Emit;

class Test
{
static void Main()
{
AssemblyName myAssemblyName = new AssemblyName();
myAssemblyName.Name = "EmittedAssembly";

AppDomain myAppDomain = AppDomain.CurrentDomain;

// Create the dynamic assembly.
AssemblyBuilder myAssemblyBuilder =
myAppDomain.DefineDynamicAssembly(myAssemblyName,
AssemblyBuilderAccess.RunAndSave);

// Create a dynamic module.
ModuleBuilder myModuleBuilder =
myAssemblyBuilder.DefineDynamicModule("EmittedModu le",
"EmittedModule.mod");

// Create a dynamic Enum.
EnumBuilder myEnumBuilder = myModuleBuilder.DefineEnum("MyEnum",
TypeAttributes.Public, typeof(Int32));

FieldBuilder myFieldBuilder1
= myEnumBuilder.DefineLiteral("FieldOne", 1);

FieldBuilder myFieldBuilder2
= myEnumBuilder.DefineLiteral("FieldTwo", 2);

Type myEnumType = myEnumBuilder.CreateType();

object anEnum = Enum.ToObject(myEnumType, 2);

TypeBuilder myTypeBuilder = myModuleBuilder.DefineType("MyType");

MethodBuilder myMethodBuilder = myTypeBuilder.DefineMethod(
"MyMethod",
MethodAttributes.Public,
typeof(void),
new Type[] { myEnumType });

ILGenerator myILGenerator = myMethodBuilder.GetILGenerator();
myILGenerator.Emit(OpCodes.Ldarg_1);
myILGenerator.Emit(OpCodes.Box, myEnumType);
MethodInfo myWriteLineMethodInfo =
typeof(Console).GetMethod("WriteLine",new Type[]{typeof(object)});
myILGenerator.Emit(OpCodes.Call, myWriteLineMethodInfo);
myILGenerator.Emit(OpCodes.Ret);

Type myType = myTypeBuilder.CreateType();

object myInstance = Activator.CreateInstance(myType);

MethodInfo myMethod = myType.GetMethod("MyMethod");

myMethod.Invoke(myInstance, new object[] { anEnum });

myAssemblyBuilder.Save("Ass1.dll");

Console.ReadLine();
}
}
Joe
--
http://www.csharp-station.com

"Steve Zissou" <__****************@hocus.pocus> wrote in message
news:mw**************@newsread3.news.pas.earthlink .net...
Joe Mayo wrote:
Hi Moe,

The problem with what you are trying to accomplish is when each part of
the code is defined. The method, and its parameters, are defined at
compile time. However, your enum is defined at run time. The only way
to get the behavior you want is to dynamically build your method at
runtime also.

Joe


Thanks, Joe.

I've seen code showing how to create the Enum dynamically at run time.

But when it comes time to utilize that as the input type of an actual
method

void myMethod ( myDynamicEnum mde) { .... }

I can't seem to make heads of tails of it !

For example, I can globally declare the Assembly at the class level, but
if I then try and define a Type using the GetType method of Assembly,
Visual Studio barfs on me.

Can it be done? It seems doable...but I'm just missing some key trick...

Jul 21 '05 #4

This is very cool stuff, Joe, thanks for your help.

I got the base working now, and I'm going to walk through your code as well.

Reflection is the coolest.

I think I read that Reflection can be performed direct into memory (not
just saving to a file)...is that true? Can I replace a loaded dll with
the new dynamic dll without going through saving to a file and calling?
Joe Mayo wrote:
Hi Steve,

Here are a few tips:

1. When you instantiate your AssemblyBuilder, you need to change its access
from AssemblyBuilderAccess.Save to AssemblyBuilderAccess.RunAndSave.

2. Get an instance of your enum via the Enum.ToObject() method.

3. Write the code for the method you want to dynamically generate. Then
use ILDASM to see the IL it generates. Use the compiler generated IL to
figure out what opcodes to use in the ILGenerator.Emit calls.

Here's an example of what you need to do:

using System;
using System.Reflection;
using System.Reflection.Emit;

class Test
{
static void Main()
{
AssemblyName myAssemblyName = new AssemblyName();
myAssemblyName.Name = "EmittedAssembly";

AppDomain myAppDomain = AppDomain.CurrentDomain;

// Create the dynamic assembly.
AssemblyBuilder myAssemblyBuilder =
myAppDomain.DefineDynamicAssembly(myAssemblyName,
AssemblyBuilderAccess.RunAndSave);

// Create a dynamic module.
ModuleBuilder myModuleBuilder =
myAssemblyBuilder.DefineDynamicModule("EmittedModu le",
"EmittedModule.mod");

// Create a dynamic Enum.
EnumBuilder myEnumBuilder = myModuleBuilder.DefineEnum("MyEnum",
TypeAttributes.Public, typeof(Int32));

FieldBuilder myFieldBuilder1
= myEnumBuilder.DefineLiteral("FieldOne", 1);

FieldBuilder myFieldBuilder2
= myEnumBuilder.DefineLiteral("FieldTwo", 2);

Type myEnumType = myEnumBuilder.CreateType();

object anEnum = Enum.ToObject(myEnumType, 2);

TypeBuilder myTypeBuilder = myModuleBuilder.DefineType("MyType");

MethodBuilder myMethodBuilder = myTypeBuilder.DefineMethod(
"MyMethod",
MethodAttributes.Public,
typeof(void),
new Type[] { myEnumType });

ILGenerator myILGenerator = myMethodBuilder.GetILGenerator();
myILGenerator.Emit(OpCodes.Ldarg_1);
myILGenerator.Emit(OpCodes.Box, myEnumType);
MethodInfo myWriteLineMethodInfo =
typeof(Console).GetMethod("WriteLine",new Type[]{typeof(object)});
myILGenerator.Emit(OpCodes.Call, myWriteLineMethodInfo);
myILGenerator.Emit(OpCodes.Ret);

Type myType = myTypeBuilder.CreateType();

object myInstance = Activator.CreateInstance(myType);

MethodInfo myMethod = myType.GetMethod("MyMethod");

myMethod.Invoke(myInstance, new object[] { anEnum });

myAssemblyBuilder.Save("Ass1.dll");

Console.ReadLine();
}
}
Joe

Jul 21 '05 #5
Sure, just use AssemblyBuilderAccess.Run when instantiating your
AssemblyBuilder and skip the call to Save().

Joe
--
http://www.csharp-station.com

"Moe Green" <mo*@green.vegas> wrote in message
news:33*************@individual.net...

This is very cool stuff, Joe, thanks for your help.

I got the base working now, and I'm going to walk through your code as
well.

Reflection is the coolest.

I think I read that Reflection can be performed direct into memory (not
just saving to a file)...is that true? Can I replace a loaded dll with
the new dynamic dll without going through saving to a file and calling?
Joe Mayo wrote:
Hi Steve,

Here are a few tips:

1. When you instantiate your AssemblyBuilder, you need to change its
access from AssemblyBuilderAccess.Save to
AssemblyBuilderAccess.RunAndSave.

2. Get an instance of your enum via the Enum.ToObject() method.

3. Write the code for the method you want to dynamically generate. Then
use ILDASM to see the IL it generates. Use the compiler generated IL to
figure out what opcodes to use in the ILGenerator.Emit calls.

Here's an example of what you need to do:

using System;
using System.Reflection;
using System.Reflection.Emit;

class Test
{
static void Main()
{
AssemblyName myAssemblyName = new AssemblyName();
myAssemblyName.Name = "EmittedAssembly";

AppDomain myAppDomain = AppDomain.CurrentDomain;

// Create the dynamic assembly.
AssemblyBuilder myAssemblyBuilder =
myAppDomain.DefineDynamicAssembly(myAssemblyName,
AssemblyBuilderAccess.RunAndSave);

// Create a dynamic module.
ModuleBuilder myModuleBuilder =
myAssemblyBuilder.DefineDynamicModule("EmittedModu le",
"EmittedModule.mod");

// Create a dynamic Enum.
EnumBuilder myEnumBuilder = myModuleBuilder.DefineEnum("MyEnum",
TypeAttributes.Public, typeof(Int32));

FieldBuilder myFieldBuilder1
= myEnumBuilder.DefineLiteral("FieldOne", 1);

FieldBuilder myFieldBuilder2
= myEnumBuilder.DefineLiteral("FieldTwo", 2);

Type myEnumType = myEnumBuilder.CreateType();

object anEnum = Enum.ToObject(myEnumType, 2);

TypeBuilder myTypeBuilder = myModuleBuilder.DefineType("MyType");

MethodBuilder myMethodBuilder = myTypeBuilder.DefineMethod(
"MyMethod",
MethodAttributes.Public,
typeof(void),
new Type[] { myEnumType });

ILGenerator myILGenerator = myMethodBuilder.GetILGenerator();
myILGenerator.Emit(OpCodes.Ldarg_1);
myILGenerator.Emit(OpCodes.Box, myEnumType);
MethodInfo myWriteLineMethodInfo =
typeof(Console).GetMethod("WriteLine",new Type[]{typeof(object)});
myILGenerator.Emit(OpCodes.Call, myWriteLineMethodInfo);
myILGenerator.Emit(OpCodes.Ret);

Type myType = myTypeBuilder.CreateType();

object myInstance = Activator.CreateInstance(myType);

MethodInfo myMethod = myType.GetMethod("MyMethod");

myMethod.Invoke(myInstance, new object[] { anEnum });

myAssemblyBuilder.Save("Ass1.dll");

Console.ReadLine();
}
}
Joe

Jul 21 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Edward Diener | last post by:
In an assembly I have a __value enum with some enumerated constants, ie. namespace X { public __value enum MyEnum { ValueA, ValueB }; }
10
by: dof | last post by:
I'm trying the following and having problems. I get errors on the array declaration lines. Something about an array must have at least one element. Thanks in advance. D #include stuff .... ...
2
by: Michel | last post by:
Hi there, I have an enum field that I serialize: public ParameterDirection Direction = ParameterDirection.Out; The enum looks like this: public enum ParameterDirection { In, Out, Both };
1
by: someone else | last post by:
I have some code that creates dynamic enumerations for use in a PropertyGrid control. This all works perfectly but the memory usage of the program increases quite quicly when viewing the...
21
by: Andreas Huber | last post by:
Hi there Spending half an hour searching through the archive I haven't found a rationale for the following behavior. using System; // note the missing Flags attribute enum Color {
5
by: Moe Green | last post by:
I want create a method which uses a dynamically enum as the type of one of its parameters. I can create the assembly with this code: AssemblyName myAssemblyName = new AssemblyName();...
13
by: Don | last post by:
How do I get an Enum's type using only the Enum name? e.g. Dim enumType as System.Type Dim enumName as String = "MyEnum" enumType = ???(enumName)
1
by: None | last post by:
I'm trying to mark up an enum in a .h file such that when it is emitted to the .idl file the elements of the enum can be marked up with helpstring attributes. Microsoft has a published sample...
12
by: Cmtk Software | last post by:
I'm trying to define an enum which will be used from unmanaged c++, C++/CLI managed c++ and from C#. I defined the following enum in a VS dll project set to be compiled with the /clr switch: ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.