定义"动态"枚举 [英] Define "dynamic" enum

查看:128
本文介绍了定义"动态"枚举的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在做这个项目,我有大量的服务来执行。我需要这个服务codeS这样我就可以verifiy并根据所选的服务做出决策通过它的code。

I'm working on this project and I have a large number of "services" to execute. I need to have this service codes so I can verifiy and make decisions according to selected services by it's code.

我特林定义这个枚举并获得从web.config它的价值,所以我可以很容易地改变它们。

I'm tring to define this enum and get its values from the web.config so I can change them easily.

public enum ServiceCodes
{
    Transfer= Convert.ToInt32(ConfigurationManager.AppSettings["servTransfer"]),
    ChangePlate= Convert.ToInt32(ConfigurationManager.AppSettings["servChangePlate"])
}

不过,我得到这个错误:EX pression必须是恒定的。 (由于转换的)

But I get this error: The expression must be constant. (because of the Conversion)

我能做些什么? (或者你可以建议我完全不同的方法。)

What can I do? (Or you can suggest me totally different approaches.)

推荐答案

只要继续下去,并定义一个静态类是这样的:

Just go ahead and define a static class like this:

public static class ServiceCodes
{
  readonly static int Transfer = Convert.ToInt32(ConfigurationManager.AppSettings["servTransfer"])
  //...
}

这篇关于定义"动态"枚举的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆