2013-10-24 45 views
0

我想讓我的枚舉類具有名稱 - 值對。我必須在我的xsd中定義枚舉。在xsd中爲枚舉元素指定值

例如: 目前,我有我的XSD作爲

<xsd:simpleType name="ColorCode"> 
     <xsd:restriction base="xsd:string"> 
     <xsd:enumeration value="Red"/> 
     <xsd:enumeration value="Orange"/> 
     <xsd:enumeration value="LightGreen"/> 
     <xsd:enumeration value="DarkGreen"/> 
     <xsd:enumeration value="LightBlue"/> 
     <xsd:enumeration value="DarkBlue"/> 
     <xsd:enumeration value="DarkGrey"/> 
     <xsd:enumeration value="LightGrey"/> 
     </xsd:restriction> 
    </xsd:simpleType> 

生成的代碼是:

[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")] 
    [System.SerializableAttribute()] 
    public enum ColorCode { 

     /// <remarks/> 
     Red, 

     /// <remarks/> 
     Orange, 

     /// <remarks/> 
     LightGreen, 

     /// <remarks/> 
     DarkGreen, 

     /// <remarks/> 
     LightBlue, 

     /// <remarks/> 
     DarkBlue, 

     /// <remarks/> 
     DarkGrey, 

     /// <remarks/> 
     LightGrey, 
    } 

我如何定義我的xsd使生成的代碼是什麼如下:

[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")] 
    [System.SerializableAttribute()] 
    public enum ColorCode { 

     /// <remarks/> 
     Red = 0x12, 

     /// <remarks/> 
     Orange = 0x13, 

     /// <remarks/> 
     LightGreen = 0x17, 

     /// <remarks/> 
     DarkGreen=0x20, 

     /// <remarks/> 
     LightBlue=0x40, 

     /// <remarks/> 
     DarkBlue=0x50, 

     /// <remarks/> 
     DarkGrey0x90, 

     /// <remarks/> 
     LightGrey=0x190, 
    } 

回答

0

前段時間我面臨同樣的問題,但找不到答案。所以我結束了這樣的工作:

private static readonly int[][] _conversion = 
     { 
      new[] {(int) ColorCode.Red, 0x12}, 
      new[] {(int) ColorCode.Orange, 0x13}, 
      new[] {(int) ColorCode.LightGreen, 0x17}, 
      ..... 
     }; 

    static int ToValue(ColorCode color) 
    { 
     if (_conversion.Any(c => c[0] == (int) color)) 
      return _conversion.First(c => c[0] == (int) color)[1]; 

     throw new NotImplementedException(string.Format("Color conversion is not in the dictionary! ({0})", color)); 
    } 

    static ColorCode ToEnum(int value) 
    { 
     if (_conversion.Any(c => c[1] == value)) 
      return (ColorCode)_conversion.First(c => c[1] == value)[0]; 

     throw new NotImplementedException(string.Format("Value conversion is not in the dictionary! ({0})", value)); 
    } 

    static void Main(string[] args) 
    { 
     Console.WriteLine(ToEnum(0x13)); 
     Console.WriteLine(ToValue(ColorCode.Red)); 
    }