Click here to Skip to main content
15,886,518 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
So I have this rather large application that uses a TagType enum. The use of TagType is widespread in the codebase. I am modding the code to allow users to add tags, in other words to "extend" the enum so to speak at Runtime

The app is a database front end. We cannot change the codebase. We cannot make significant changes so as to render the codebase "untested". Therefore we cannot implement what might be considered "best practice". We simply need a way to "extend" the type - at runtime - so that the codebase continues to function "as was" but with the ability to add a small piece of code that will implement - by whatever means possible - this change

current definition of TagType is

C#
public enum TagType {
   __RESERVED__ = 0,
   __Raised__   = 1,
   __Closed__   = 2 
}


but we need some way of "extending" this values at runtime so that the codebase functionality remains runnable , i.e. where the code might for example test:
C#
if (localTag == __Raised__)
   db.Search(localTag)


and so on ...

your solution doesn't need to be an enum, the TagType is in its own file and we can change the implementation of TagType to anything PROVIDED the rest of the codebase compiles and runs.

By the way, the code needs to be portable and not make assumptions on O/S - so reflection based solutions might not be possible.

What I have tried:

I scour of google pages reveals many solutions to individual situations but having read these, none of them I have read appear to fit this requirement. So please, if you intend to refer me to google then please can I ask very politely that you go an help some other codeproject member rather than post here.

Also, this looked like a good solution, however it is dependent on assemblies and I didn't want to force the code to a particular os.

tps://stackoverflow.com/questions/857414/dynamically-create-an-enum
Posted
Updated 13-Jul-17 3:51am
v3
Comments
F-ES Sitecore 13-Jul-17 8:49am    
If the code doesn't know about your extended enum values when it was compiled then it can't do anything with them so this requirement is a little pointless.

Refactor the code to use something other than an enum, use a name value collection, a dictionary, or something else that suits the requirement. Saying you can't change the codebase is like saying you want to make an omelette without breaking any eggs.
ninjaef 13-Jul-17 10:05am    
Incorrect assumption

The codebase is plugin orientated, albeit that the plugins can be quite large in terms of SLoC. It is the plugin we are responsible for. So the "interpretation" of "enums" is performed in the plugin but distributed via CallBacks. I don't want to get into the design which is why I purposely requested help on a very specific example , and which is why I also did not want to get into a debate on an architecture and design model that we here have little if any control over.

1 solution

One way would be:
C#
 public enum TagType {
   __RESERVED__ = 0,
   __Raised__   = 1,
   __Closed__   = 2 
}
TagType AnotherEnumValue = (TagType)3;

You can later check it like:
C#
if (SomeValue == (TagType)3{

}

I hope this will help you out!
 
Share this answer
 
Comments
ninjaef 13-Jul-17 10:11am    
simple and perfect. will give this a go but looks like an elegant solution to extend extant functionality.

Two question though:-
1.the enum.IsDefined() requires a string argument. Is there a simple way to check if an int value is defined within the enum.
2.TagType AnotherEnumValue = (TagType)3 would not "add" the "AnotherEnumValue" to the enum range - in other words IsDefined(3) would yield False? How might we overcome this?
Prifti Constantine 13-Jul-17 10:17am    
You could use some Reflection In this situation so you can get the type of the Enum Specific Value and check it wherever you want! If you need a further explanation i could provide it...even though you could only use TypeOf(). This will temporarily add the value you want inside the Enum for as long as the project stays in runtime... It is not going to Generate Code for you... In this situation "F-ES Sitecore user" is right.. You could use some type of collection which is more handy in these situations! Glad to help! It would really help if you validated the answer! Best regards!
ninjaef 13-Jul-17 10:20am    
example of how I might use collection would be nice?

using the TagType example above?
Prifti Constantine 13-Jul-17 10:22am    
What exactly are you trying to achieve with this Enum?
ninjaef 13-Jul-17 12:38pm    
as explained in the OP

not sure what you don't understand? sorry.

but its simple

we have a Tagtype Enum used in lot of code. we need to introduce a mechanism to allow the list of enumerations to be added to at runtime whilst maintaining code references to Tag type, such as

if (a == TagType.someEnum)...

simple

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900