C#:" extend"运行时的枚举类型 [英] C# : "extend" an enum type at runtime

查看:159
本文介绍了C#:" extend"运行时的枚举类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个使用TagType枚举的相当大的应用程序。 TagType的使用在代码库中很普遍。我正在修改代码以允许用户添加标签,换句话说,扩展枚举,以便在运行时发言



该应用程序是数据库前端。我们无法更改代码库。我们无法进行重大更改,以使代码库未经测试。因此,我们无法实施可能被视为最佳实践的内容。我们只需要一种方法来扩展类型 - 在运行时 - 以便代码库继续按原样运行,但是能够添加一小段代码来实现 - 通过任何可能的方式 - 这种改变



当前TagType的定义是



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

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:

if (localTag == __Raised__)
   db.Search(localTag)





等......



你的解决方案不需要是e num,TagType在它自己的文件中,我们可以将TagType的实现更改为PROVIDED其余代码库编译和运行的任何内容。



顺便说一下,代码需要是可移植的,而不是对O / S做出假设 - 所以基于反射的解决方案可能是不可能的。



我尝试了什么:



我在谷歌网页上的搜索揭示了针对个别情况的许多解决方案,读过这些之后,我读过的所有这些都不符合这个要求。所以,如果您打算将我推荐给Google,那么我可以非常礼貌地问你是否需要帮助其他代码项目成员而不是在这里发帖。



此外,这看起来是一个很好的解决方案,但它依赖于程序集,我不想强​​制代码到特定的操作系统。



tps:// stackoverflow。 com / questions / 857414 / dynamic-create-an-enum



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

推荐答案

一种方法是:

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



您可以稍后查看:


You can later check it like:

if (SomeValue == (TagType)3{

}



我希望这能帮到你!


I hope this will help you out!


这篇关于C#:" extend"运行时的枚举类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆