如何在C#中转换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;
    }
};}

根据我所知道的编译器,以上代码是不正确的,但是如何使它工作。请帮忙。谢谢。

参考方案

这是您的方法的有效版本:

// 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;
    }
}

如何在C#上使用参数调用JavaScript函数 - c#

我想用C#中的HttpWebRequest或WebRequest调用JavaScript函数。我不想使用可以称为invokemember的Web浏览器。这是我的代码:public void MyWebRequest(string url, string method, string data) { request = WebRequest.Create(ur…

如何在C#中的循环过程中多次更改控件的属性 - c#

我正在尝试做这样的事情:int i; while(true) { label1.Text = i.ToString(); i++; Thread.Sleep(500); } (实际上,我正在尝试做更复杂的事情,这更有意义,但这只是我的问题的一个简单示例)我期望标签的文本每1/2秒更改一次..但是它被卡住了。谢谢 参考方案 您不能让GUI线程进入睡眠状态(因为…

如何在C#Linq中找到集合的最小差异对象 - c#

我有收藏Class MyData { int f1; int f2; int f3; int f4; } var mycollection =List<MyData>(); 我需要返回字段f1和f3之间具有最小差异的对象。我在下面查询了mycollection.select(obj => obj.f1 - obj.f3).Min(); 但是…

如何在C#中使用Telegram API发送消息 - c#

我想在C#中使用Telegram API将简单消息发送给号码。我在GitHub上找到了一些lib,但是我无法使用它们。谁能给一个简单的代码?我可以简单地进行HTTP调用吗? 参考方案 安装包电报使用Botfather创建一个Bot使用/ token命令获取api密钥(仍在botfather中)使用此代码:var bot = new Api("you…

如何在C#中以正确的方式传递参数? - c#

我的存储过程如下:ALTER PROCEDURE [dbo].[Insert_tblCustomer] -- Add the parameters for the stored procedure here @Username varchar(20) = null, @Password varchar(20)= null, @UserType varchar…