|
-
September 19th, 2008, 10:39 AM
#1
Dynamic enum
Hi guys,
here is the idea. I'm writing a piece of code that performs different actions based on type parameter. The types are obviously encapsulated in enum in C# code. This is all OK except that I'm hard coding actions list in enum - therefore upon requirement to add another action C# code needs to be modified and recompiled and this means that whole application needs to be redeployed, downtime needs to be scheduled etc, etc.
So I would like to have a list of options in my .config file and dynamically build a enum out of them. How this can be done? Any advice, suggestion or criticism is welcome.
Thanks,
Tommy
PS
I'm using .NET 2.0
-
September 19th, 2008, 11:08 AM
#2
Re: Dynamic enum
You can parse a string to an enum using:
Code:
MyEnumType myEnumObj = (MyEnumType)Enum.Parse(typeof(MyEnumType), myString);
So you can store the enumerations as strings in the config file, and parse them into their respective enum values at runtime as you see fit. Just remember to put error checking in place in case the parse fails. Hopefully this will help a bit
Last edited by RaleTheBlade; September 19th, 2008 at 11:18 AM.
R.I.P. 3.5" Floppy Drives
"I know not with what weapons World War III will be fought, but World War IV will be fought with sticks and stones." - Albert Einstein
-
September 19th, 2008, 01:27 PM
#3
Re: Dynamic enum
 Originally Posted by RaleTheBlade
You can parse a string to an enum using:
Code:
MyEnumType myEnumObj = (MyEnumType)Enum.Parse(typeof(MyEnumType), myString);
So you can store the enumerations as strings in the config file, and parse them into their respective enum values at runtime as you see fit. Just remember to put error checking in place in case the parse fails. Hopefully this will help a bit 
You can't. You need to have predefined every member of the enum, which is not what this guy wants to do.
If you want to avoid the hassle of trying to keep everything in sync, then change the architecture so you don't need to have an enum. I take it that you're doing something like:
Code:
if(myEnum == ClassType.X)
DoX(myobject);
else if (myEnum == ClassType.Y)
DoY(myobject);
else if (myEnum == ClassType.Z)
DoZ(myobject);
If so, why don't you just make all classes implement an interface/abstract class which defines a 'Do' method, so you can just call:
and then implement DoY () inside the Y class and DoX inside the X class etc. Of course, to add a new type you still need to recompile the app (or have a plugin framework of some kind), but it's still much easier than before.
www.monotorrent.com For all your .NET bittorrent needs
NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.
-
September 19th, 2008, 03:03 PM
#4
Re: Dynamic enum
What you are wanting to do is normally done using design patterns and interfaces just as Mutant suggested you to do ! This enables you to load any class which has the interface implemented independend what other things this class will do for you, or what other classes in the future will do for you as long as they have implemented the Interface with the Do methd. Switching in such cases is always a dead end, as the day will come where you need to recompile your code and doing bigger changes.
BTW I have read a very funny book on design patterns. Its named Head First DesignPatterns. Only bitter point is it is in Java but this isn't very difficult to translate to C# IMHO as patterns are patterns. I've more looked to the idea behind then to the code itself. But in the end, as I told its pretty similar.
 Jonny Poet
To be Alive is depending on the willingsness to help others and also to permit others to help you. So lets be alive. !
Using Code Tags makes the difference: Code is easier to read, so its easier to help. Do it like this: [CODE] Put Your Code here [/code]
If anyone felt he has got help, show it in rating the post.
Also dont forget to set a post which is fully answered to 'resolved'. For more details look to FAQ's about Forum Usage. BTW I'm using Framework 3.5 and you ?
My latest articles :
Creating a Dockable Panel-Controlmanager Using C#, Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7
-
September 22nd, 2008, 11:10 AM
#5
Re: Dynamic enum
Thanks guys
-
September 23rd, 2008, 10:36 PM
#6
Re: Dynamic enum
Another option for you is to save actions in database i.e. some string column in some table. And load the Actions list from database. If you are required to add another action, add it in database and it will be loaded and passed to your business logic automatically by your data loading code.
Drawback is that you will have to leave the idea of using Enum.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|