在C#中运行时设置枚举值 [英] Setting enum value at runtime in C#

查看:166
本文介绍了在C#中运行时设置枚举值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在运行时更改枚举的值?

Is there any way that I can change enum values at run-time?

例如,我已经关注了类型

e.g I have following type

enum MyType
{
   TypeOne, //=5 at runtime 
   TypeTwo  //=3 at runtime
}

我希望在运行时将5设置为 TypeOne 和3到 TypeTwo

I want at runtime set 5 to TypeOne and 3 to TypeTwo.

推荐答案

只需参考MSDN帮助这里

Just refer to MSDN help HERE


  • 枚举类型(也称为枚举或枚举)提供了一种有效的方法来定义可以分配给变量的一组命名积分常数。

HERE

在健壮的编程部分中-与任何常量一样,所有对枚举值的引用都将转换为数字常量编译时间

In the Robust Programming Section - Just as with any constant, all references to the individual values of an enum are converted to numeric literals at compile time.

因此,您需要重新调整 Enum 的概念,并相应地使用它

So you need to realign your idea of Enum and use it accordingly.

要回答您的问题-不可能。

To answer your question - No it is not possible.

这篇关于在C#中运行时设置枚举值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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