简体   繁体   English

十进制值/名称的枚举标志转换

[英]Enum Flags conversions for Decimal values/names

I have few enum flags that represent a version of an external system . 我没有几个表示外部系统版本的 枚举标志 I want to ensure, that every time the external system upgrades its version, my functionality should be able to support it (I will know in advance that version will be upgraded, so its a proactive action). 我想确保,每次外部系统升级其版本时,我的功能都应该能够支持它(我将事先知道该版本将被升级,因此将采取主动行动)。 Also my communication has to be in both directions with this system, which means internal and external communication can have different version sets. 另外,我与该系统的通信必须双向,这意味着内部和外部通信可以具有不同的版本集。

I have created an enum flag, to represent these versions. 我创建了一个枚举标志来代表这些版本。

[Flags]
public enum ExternalSystemVersionEnum
{
    System1 = 1,
    System2 = 2,
    System3 = 4,
    All = 7
}

This works okay for a formal major upgrades. 进行正式的重大升级就可以了。 Sadly I can't name those as System1.0 or so on. 可悲的是,我不能将它们命名为System1.0等。 Also, as its decimals, I can't use generic enum parser. 另外,作为小数,我不能使用通用枚举解析器。

(T) Enum.Parse(typeof (T), Convert.ToString(enumValue));

Of course I can still achieve the above by using switch/if statements instead, but I really want to avoid any hard coding at all (even Resources/Contants if I can), as one version of external system may get completely discarded, and I don't want to be bothered about having to chase down all strings. 当然,我仍然可以通过使用switch / if语句来实现上述目的,但是我真的想完全避免进行任何硬编码 (如果可以的话,甚至可以使用资源/内容),因为一个外部系统版本可能会被完全丢弃,而我不想被追逐所有字符串所困扰。

Also I need Flags or equivalent functionality, as I may still receive communication that has no System version Identification, which by default goes to applicable to all versions . 另外,我还需要标志或等效功能,因为我仍然会收到没有系统版本标识的通信,默认情况下,该标识适用于所有版本 Another communication may just apply to some select versions , eg. 另一种通信可能仅适用于某些选定版本 ,例如。 only for 2.0 onward etc. 仅适用于2.0及更高版本。

Please note, I can easily achieve this by using Switch/If, but I don't wish to have any hard coding, unless there's no other (simple) way. 请注意,我可以通过使用Switch / If轻松实现此目的,但是我不希望有任何硬编码,除非没有其他(简单)方法。 My main concern here is having a functionality that acts like flags , yet can easily identifies values with decimal places , say 1.0 or 1.2 or 1.3.1 for that matter (though in history they've never had double decimal points), and provides compile time failures . 我在这里主要关心的是功能类似于标志的功能,但是可以很容易地标识带有小数位的值 ,例如1.0或1.2或1.3.1(尽管历史上它们从来没有双小数点),并提供编译时间的失败 I am open to suggestions of other mechanisms, but I would really prefer it to be a simple logic, as its a very trivial part of the entire process. 我乐于接受其他机制的建议,但我真的希望它是一个简单的逻辑,因为它是整个过程中非常琐碎的部分。

What about 关于什么

[Flags]
public enum ExternalSystemVersionEnum
{
    System1_1 = 1,
    System2_1 = 2,
    System3_2 = 4,
    All = 7
}

var enumString = decimalValue.ToString("0.0", 
    CultureInfo.InvarantCulture).Replace('.', "_");

var enumValue = (ExternalSystemVersionEnum)Enum.Parse(
    typeof(ExternalSystemVersionEnum), enumString);

It sounds like these values could be changing depending on what systems are connected and if the connected systems will be upgraded. 听起来这些值可能会变化,具体取决于连接的系统以及连接的系统是否要升级。 In order to remove the hardcoding and to make it more flexible I would suggest adding your data as a custom section in an app.config file. 为了删除硬编码并使其更加灵活,建议您将数据作为自定义部分添加到app.config文件中。 In that way you can easily modify/add/remove the values without the need to recompile every time something changes. 这样,您可以轻松地修改/添加/删除值,而无需每次发生更改时都重新编译。

<configSections>
    <section name="SystemVersions" type="YourAppName.SystemVersionSection, YourAppName" />
</configSections>

<SystemVersions>
    <system version="1.0" />
    <system version="2.0" />
    <system version="1.3.1" />
    <system version="All" />
</SystemVersions>

This CodeProject page Custom Configuration Sections for Lazy Coders may help you. 此CodeProject页面的“惰性编码器的自定义配置”部分可能会为您提供帮助。

I understand that this doesn't quite fit your requirements of using an Enum, however sometimes an alternative design will be a better fit overall. 我知道这不太符合您使用Enum的要求,但是有时替代设计会更适合整体。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM