2013-09-28 60 views
0

我已經搜索並找到很多示例,但仍然需要專家的幫助。 低於在Java代碼:如何在C#中轉換java枚舉#

public class myClass { 

    public static enum myEnum { 
    P("aco", 1000, 4, 8), L("acs", 2100, 
      1, 9), S("acn", 3500, 1, 6), H("ach", 5400, 1, 6); 

    final public String cc; 
    final int bt; 
    final int Qp; 
    final int lp; 

    private myEnum(String cc, int bt, int Qp, int lp) { 
     this.cc = cc; 
     this.bt = bt; 
     this.Qp = Qp; 
     this.lp = lp; 
    } 
};} 

我試圖將其轉換像下面通過看實例:

using System.Reflection; 
using System; 

[System.AttributeUsage(System.AttributeTargets.Field, AllowMultiple = false, Inherited = true)] 
public class EnumAttr : System.Attribute 
{ 
    public EnumAttr() 
    { 
    }  
} 

public static class EnumExtension 
{ 
    public static EnumAttr GetAttr(this Enum value) 
    { 
     Type type = value.GetType(); 
     FieldInfo fieldInfo = type.GetField(value.ToString()); 
     var atts = (EnumAttr[])fieldInfo.GetCustomAttributes(typeof(EnumAttr), false); 
     return atts.Length > 0 ? atts[0] : null; 
    } 
} 

public class myEnumAttr : EnumAttr 
{ 
    public string Desc { get; set; } 
} 

public class myClass 
{ 
    public enum myEnum 
    { 
     [myEnumAttr("aco", 1000, 4, 8)]P, 
     [myEnumAttr("acs", 2100, 1, 9)]L, 
     [myEnumAttr("acn", 3500, 1, 6)]S, 
     [myEnumAttr("ach", 5400, 1, 6)]H, 

    public String cc; 
    int bt; 
    int Qp; 
    int lp; 

    private void myEnum(String cc, int bt, int Qp, int lp) { 
     this.cc = cc; 
     this.bt = bt; 
     this.Qp = Qp; 
     this.lp = lp; 
    } 
};} 

上面的代碼是不正確的,因爲每個編譯器,但我知道如何使它工作。請幫忙。謝謝。

回答

3

這是你的方法的一個工作版本:

// use like 
string desc = myEnum.P.GetAttr().Desc; 

[System.AttributeUsage(System.AttributeTargets.Field, AllowMultiple = false, Inherited = true)] 
public class EnumAttr : System.Attribute 
{ 
    public EnumAttr() 
    { 
    }  
} 

public static class EnumExtension 
{ 
    public static EnumAttr GetAttr(this Enum value) 
    { 
     Type type = value.GetType(); 
     FieldInfo fieldInfo = type.GetField(value.ToString()); 
     var atts = (EnumAttr[])fieldInfo.GetCustomAttributes(typeof(EnumAttr), false); 
     return atts.Length > 0 ? atts[0] : null; 
    } 
    public static myEnumAttr GetAttr(this myEnum value) 
    { 
     Type type = value.GetType(); 
     FieldInfo fieldInfo = type.GetField(value.ToString()); 
     var atts = (myEnumAttr[])fieldInfo.GetCustomAttributes(typeof(myEnumAttr), false); 
     return atts.Length > 0 ? atts[0] : null; 
    } 
} 

public class myEnumAttr : EnumAttr 
{ 
    public string Desc { get; set; } 

    int bt; 
    int Qp; 
    int lp; 

    public myEnumAttr(String desc, int bt, int Qp, int lp) { 
     this.Desc = desc; 
     this.bt = bt; 
     this.Qp = Qp; 
     this.lp = lp; 
    } 
} 

public enum myEnum 
{ 
    [myEnumAttr("aco", 1000, 4, 8)]P, 
    [myEnumAttr("acs", 2100, 1, 9)]L, 
    [myEnumAttr("acn", 3500, 1, 6)]S, 
    [myEnumAttr("ach", 5400, 1, 6)]H, 
} 

不過,我不知道這是解決這個問題的最好辦法。也許使用private構造函數來使用類的實例會更有意義。

// use like 
string desc = MyEnumLike.P.Desc; 

public sealed class MyEnumLike 
{ 
    public static readonly MyEnumLike P = new MyEnumLike("aco", 1000, 4, 8); 
    public static readonly MyEnumLike L = new MyEnumLike("acs", 2100, 1, 9); 
    public static readonly MyEnumLike S = new MyEnumLike("acn", 3500, 1, 6); 
    public static readonly MyEnumLike H = new MyEnumLike("ach", 5400, 1, 6); 

    public string Desc { get; set; } 

    int bt; 
    int Qp; 
    int lp; 

    private MyEnumLike(String desc, int bt, int Qp, int lp) { 
     this.Desc = desc; 
     this.bt = bt; 
     this.Qp = Qp; 
     this.lp = lp; 
    } 
} 
+0

謝謝。我瞭解解決方法。 –