java的枚举转c#

有段java的enum定义

public enum ConstantKind {
 
    CONSTANT_Int (0x03),
    CONSTANT_UInt (0x04),
    CONSTANT_Double (0x06),
    CONSTANT_Utf8 (0x01),
    CONSTANT_True (0x0B),
    CONSTANT_False (0x0A),
    CONSTANT_Null (0x0C),
    CONSTANT_Undefined (0x00),
    CONSTANT_Namespace (0x08),
    CONSTANT_PackageNamespace (0x16),
    CONSTANT_PackageInternalNs (0x17),
    CONSTANT_ProtectedNamespace (0x18),
    CONSTANT_ExplicitNamespace (0x19),
    CONSTANT_StaticProtectedNs (0x1A),
    CONSTANT_PrivateNs (0x05);
     
    private static Map<Integer, ConstantKind> constantKindMap = new HashMap<Integer, ConstantKind>();
     
    static {
        for (ConstantKind constantKind : ConstantKind.values()) {
            constantKindMap.put(constantKind.constantKind, constantKind);
        }
    }
 
    private final int constantKind;
 
    ConstantKind(int constantKind){
        this.constantKind =  constantKind;
    }
 
    public int getConstantKind() {
        return constantKind;
    }
     
    public static ConstantKind getConstant(int constantKind){
        return constantKindMap.get(constantKind);
    }
     
    public Object getValueReal(int vIndex, CpoolInfo cpool){
         
        switch(this){
            case CONSTANT_Int: return cpool.getInts().get(vIndex);
            case CONSTANT_UInt: return cpool.getUints().get(vIndex);
            case CONSTANT_Double: return cpool.getDoubles().get(vIndex);
            case CONSTANT_Utf8: return cpool.getStrings().get(vIndex);
            case CONSTANT_True: return true;
            case CONSTANT_False: return false;
            case CONSTANT_Null: return null;
            case CONSTANT_Undefined: return null;
            case CONSTANT_Namespace: return cpool.getNamespaces().get(vIndex);
            case CONSTANT_PackageNamespace: return cpool.getNamespaces().get(vIndex);
            case CONSTANT_PackageInternalNs: return cpool.getNamespaces().get(vIndex);
            case CONSTANT_ProtectedNamespace: return cpool.getNamespaces().get(vIndex);
            case CONSTANT_ExplicitNamespace: return cpool.getNamespaces().get(vIndex);
            case CONSTANT_StaticProtectedNs: return cpool.getNamespaces().get(vIndex);
            case CONSTANT_PrivateNs: return cpool.getNamespaces().get(vIndex);
        }
         
        return null;
    }
     
}

并有一个调用的方法写法如下:

public String getVkind_string() {
    ConstantKind cKind = ConstantKind.getConstant(vkind);
    if(cKind != null){
        return ConstantKind.getConstant(vkind).name();
    }else{
        return "";
    }
}

如果翻译到C#, 可以这么写:

    public enum ConstantKind
    {
        CONSTANT_Int = 0x03,
        CONSTANT_UInt = 0x04,
        CONSTANT_Double = 0x06,
        CONSTANT_Utf8 = 0x01,
        CONSTANT_True = 0x0B,
        CONSTANT_False = 0x0A,
        CONSTANT_Null = 0x0C,
        CONSTANT_Undefined = 0x00,
        CONSTANT_Namespace = 0x08,
        CONSTANT_PackageNamespace = 0x16,
        CONSTANT_PackageInternalNs = 0x17,
        CONSTANT_ProtectedNamespace = 0x18,
        CONSTANT_ExplicitNamespace = 0x19,
        CONSTANT_StaticProtectedNs = 0x1A,
        CONSTANT_PrivateNs = 0x05
}

调用时这么写

public String getVkind_string() {
    ConstantKind cKind; 
    Enum.TryParse<ConstantKind>(vkind.ToString("x"), true, out cKind);
    if(cKind != null){
        return Enum.GetName(typeof(ConstantKind), cKind);     
      }else{
        return "";
    }
}

发表评论

电子邮件地址不会被公开。 必填项已用 * 标注


*

您可以使用这些 HTML 标签和属性: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>