同时维护两边的enum估计有点烦(写起来也有点烦),写了个直接调来用,

不过,客户端写代码时引用枚举就不那么直观了……

 

public static string GetJsonEnum(Type enumType)
{
    
return GetJsonEnum(enumType, null);
}

public static string GetJsonEnum(Type enumType, string alias)
{
    
int[] values = (int[])Enum.GetValues(enumType);
    
string[] names = Enum.GetNames(enumType);
    
string[] pairs = new string[values.Length];

    
for (int i = 0; i < values.Length; i++)
    {
        pairs[i] 
= names[i] + ": " + values[i];
    }

    
if (string.IsNullOrEmpty(alias))
        alias 
= enumType.Name;

    
return string.Format("var {0}={{\n{1}\n}}", alias, string.Join(",\n", pairs));
}


aspx页面上调用方式:

<%=GetJsonEnum(typeof(LYL.Test.Domain.ProductTypeEnum))%>

 

页面执行时生成结果:

var ProductEnum={
TypeA
: 1,
TypeB
: 2,
TypeC
: 3
}

 

That's all.

 

 

相关文章:

  • 2022-12-23
  • 2021-05-03
  • 2021-10-25
  • 2022-03-03
猜你喜欢
  • 2022-12-23
  • 2021-10-30
  • 2021-09-19
  • 2022-01-21
  • 2021-09-08
  • 2022-02-26
  • 2021-09-27
相关资源
相似解决方案