Using dynamic enum as type in a parameter of a method
-
Hi Experts, What i am trying to achieve here is a bit tricky. Let me brief on a little background first before going ahead. I am aware that we can use a enum as a type to a parameter of a method. For example I can do something like this (a very basic example)
namespace Test
{
class DefineEnums
{
public enum MyEnum
{
value1 = 0,
value2 = 1
}
}
class UseEnums
{
public void UseDefinedEnums(DefineEnums.MyEnum _enum)
{
//Any code here.
}public void Test() { // "value1" comes here with the intellisense. UseDefinedEnums(DefineEnums.MyEnum.value1); } }
}
What i need to do is create a dynamic Enum and use that as type in place of
DefineEnums.MyEnum
mentioned above. I tried the following. 1. Used a method which i got from the net to create a dynamic enum from a list of strings. And created a static class which i can use.using System;
using System.Collections.Generic;
using System.Reflection;
using System.Reflection.Emit;namespace Test
{
public static class DynamicEnum
{public static Enum finished; static List<string> \_lst = new List<string>(); static DynamicEnum() { \_lst.Add("value1"); \_lst.Add("value2"); finished = CreateDynamicEnum(\_lst); } public static Enum CreateDynamicEnum(List<string> \_list) { // Get the current application domain for the current thread. AppDomain currentDomain = AppDomain.CurrentDomain; // Create a dynamic assembly in the current application domain, // and allow it to be executed and saved to disk. AssemblyName aName = new AssemblyName("TempAssembly"); AssemblyBuilder ab = currentDomain.DefineDynamicAssembly( aName, AssemblyBuilderAccess.RunAndSave); // Define a dynamic module in "TempAssembly" assembly. For a single- // module assembly, the module has the same name as the assembly. ModuleBuilder mb = ab.DefineDynamicModule(aName.Name, aName.Name + ".dll"); // Define a public enumeration with the name "Elevation" and an // underlying type of Integer. EnumBuilder eb = mb.DefineEnum("Elevation", TypeAttributes.Public, typeof(int)); /
-
Hi Experts, What i am trying to achieve here is a bit tricky. Let me brief on a little background first before going ahead. I am aware that we can use a enum as a type to a parameter of a method. For example I can do something like this (a very basic example)
namespace Test
{
class DefineEnums
{
public enum MyEnum
{
value1 = 0,
value2 = 1
}
}
class UseEnums
{
public void UseDefinedEnums(DefineEnums.MyEnum _enum)
{
//Any code here.
}public void Test() { // "value1" comes here with the intellisense. UseDefinedEnums(DefineEnums.MyEnum.value1); } }
}
What i need to do is create a dynamic Enum and use that as type in place of
DefineEnums.MyEnum
mentioned above. I tried the following. 1. Used a method which i got from the net to create a dynamic enum from a list of strings. And created a static class which i can use.using System;
using System.Collections.Generic;
using System.Reflection;
using System.Reflection.Emit;namespace Test
{
public static class DynamicEnum
{public static Enum finished; static List<string> \_lst = new List<string>(); static DynamicEnum() { \_lst.Add("value1"); \_lst.Add("value2"); finished = CreateDynamicEnum(\_lst); } public static Enum CreateDynamicEnum(List<string> \_list) { // Get the current application domain for the current thread. AppDomain currentDomain = AppDomain.CurrentDomain; // Create a dynamic assembly in the current application domain, // and allow it to be executed and saved to disk. AssemblyName aName = new AssemblyName("TempAssembly"); AssemblyBuilder ab = currentDomain.DefineDynamicAssembly( aName, AssemblyBuilderAccess.RunAndSave); // Define a dynamic module in "TempAssembly" assembly. For a single- // module assembly, the module has the same name as the assembly. ModuleBuilder mb = ab.DefineDynamicModule(aName.Name, aName.Name + ".dll"); // Define a public enumeration with the name "Elevation" and an // underlying type of Integer. EnumBuilder eb = mb.DefineEnum("Elevation", TypeAttributes.Public, typeof(int)); /
Hi Experts, Any suggestions on this? Regards, Samar
-
Hi Experts, Any suggestions on this? Regards, Samar
Don't be so damn impatient. :mad: We are here voluntarily, this is not a paid support forum. You can't access the finished enum because it doesn't exit. You cut & pasted the MSDN example which names the enum Elevation.
I know the language. I've read a book. - _Madmatt
-
Don't be so damn impatient. :mad: We are here voluntarily, this is not a paid support forum. You can't access the finished enum because it doesn't exit. You cut & pasted the MSDN example which names the enum Elevation.
I know the language. I've read a book. - _Madmatt
Hi Mark, I am not impatient. I just wanted everyone to know that this thread is active and not dead. Will take care the next time. I know the enum "finished" does not exists. That is why i tried and separated the declaration and the caller in separate assemblies and gave reference of the declaration assembly in the caller project. This did not work either. Also I have clearly mentioned in the post that the method is not built by me and that i have "Used a method which i got from the net". Last words, it would be appreciated if you use a better language than using words like "damn". This is a forum and not your home. Regards, Samar
-
Hi Mark, I am not impatient. I just wanted everyone to know that this thread is active and not dead. Will take care the next time. I know the enum "finished" does not exists. That is why i tried and separated the declaration and the caller in separate assemblies and gave reference of the declaration assembly in the caller project. This did not work either. Also I have clearly mentioned in the post that the method is not built by me and that i have "Used a method which i got from the net". Last words, it would be appreciated if you use a better language than using words like "damn". This is a forum and not your home. Regards, Samar
dashingsidds wrote:
I just wanted everyone to know that this thread is active and not dead
Threads don't die. If someone has an answer, or chooses to respond, they will. You adding a response just shows you are impatient and demanding. Again, these forums are not paid support but are voluntary.
dashingsidds wrote:
use a better language than using words like "damn"
I'll damn well use damn wherever I please damnit. Since you are rude and impatient and can't follow the guidelines and etiquette of this site, you're on your own now.
I know the language. I've read a book. - _Madmatt
-
dashingsidds wrote:
I just wanted everyone to know that this thread is active and not dead
Threads don't die. If someone has an answer, or chooses to respond, they will. You adding a response just shows you are impatient and demanding. Again, these forums are not paid support but are voluntary.
dashingsidds wrote:
use a better language than using words like "damn"
I'll damn well use damn wherever I please damnit. Since you are rude and impatient and can't follow the guidelines and etiquette of this site, you're on your own now.
I know the language. I've read a book. - _Madmatt
-
The original poster just posted a reminder and did not specifically demand an answer from "Mark Nischalke" and the language was far more superior than your reply. You could have altogether ignored the post.
Shameel wrote:
posted a reminder
Obvuously you are as ignorant of proper ettique here as the OP was.
Shameel wrote:
You could have altogether ignored the post.
As could you have ignored this post as well. So what's your point?
I know the language. I've read a book. - _Madmatt
-
Don't be so damn impatient. :mad: We are here voluntarily, this is not a paid support forum. You can't access the finished enum because it doesn't exit. You cut & pasted the MSDN example which names the enum Elevation.
I know the language. I've read a book. - _Madmatt
He also cross-posted -- another no-no.