作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我已经搜索并找到了很多例子,但我仍然需要专家的帮助。下面是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# - 如何在c#中转换java枚举,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19071875/
我是一名优秀的程序员,十分优秀!