我怎样才能枚举包含一个破折号( - )?破折号

由网友(旧时光是个傻逼)分享简介:我生成从这个模式使用企业架构师。I'm generating business objects from this schema using Enterprise Architect.该模式具有以下列举说明:The schema has the following enumeration specificatio...

我生成从这个模式使用企业架构师。

I'm generating business objects from this schema using Enterprise Architect.

该模式具有以下列举说明:

The schema has the following enumeration specification:

<xs:simpleType name="PackageMedium">
    <xs:restriction base="xs:string">
        <xs:enumeration value="NTP"/>
        <xs:enumeration value="DAT"/>
        <xs:enumeration value="Exabyte"/>
        <xs:enumeration value="CD-ROM"/>
        <xs:enumeration value="DLT"/>
        <xs:enumeration value="D1"/>
        <xs:enumeration value="DVD"/>
        <xs:enumeration value="BD"/>
        <xs:enumeration value="LTO"/>
        <xs:enumeration value="LTO2"/>
        <xs:enumeration value="LTO4"/>
    </xs:restriction>
</xs:simpleType>

企业架构师生成以下code,但Visual Studio的不喜欢破折号( - )。在CD-ROM,并不会编译

Enterprise architect generates the following code but Visual Studio doesn't like the dash(-) in CD-ROM and will not compile.

public enum PackageMedium : int {
    NTP,
    DAT,
    Exabyte,
    CD-ROM,
    DLT,
    D1,
    DVD,
    BD,
    LTO,
    LTO2,
    LTO4
}

我能做些什么,使这项工作?

从枚举检索这些特殊字符。

based on @Craig Stuntz answer i was able to find this article which helped me retrieve these special characters from the Enum.

推荐答案

您不能。句号。但是,也有变通办法。你可以,例如,使用 DescriptionAttribute

You can't. Full stop. However, there are workarounds. You can, e.g., use DescriptionAttribute:

public enum PackageMedium : int {
    NTP,
    DAT,
    Exabyte,
    [Description("CD-ROM")]
    CDROM,
    DLT,
    D1,
    DVD,
    BD,
    LTO,
    LTO2,
    LTO4
}

这意味着,不幸的是,你有更多的工作映射值的时候做的。另一方面,它在免得编译。

This means, unfortunately, that you have more work to do when mapping values. On the other hand, it at lest compiles.

如果你不喜欢的是,选择其他的解决方法,例如,一本字典:

If you don't like that, pick another workaround, e.g., a dictionary:

var dict = Enum.GetValues(typeof(PackageMedium))
               .Cast<PackageMedium>()
               .Select(v => Tuple.Create(v == PackageMedium.CDROM ? "CD-ROM" : v.ToString(), v))
               .ToDictionary(t => t.Item1, t => t.Item2);

var myEnumVal = dict["CD-ROM"];
阅读全文

相关推荐

最新文章