动态改变C#中枚举的值?

发布于 2024-12-11 04:25:09 字数 190 浏览 3 评论 0原文

我有一个

    public enum TestType:int
   {
      Aphasia = 2,
      FocusedAphasia = 5
   }

设置了值的枚举。我想将枚举“FocusedAphasia”的值从 5 更改为 10。任何人都可以帮助我在运行时更改枚举的值

I have a enum

    public enum TestType:int
   {
      Aphasia = 2,
      FocusedAphasia = 5
   }

with values set. I want to change the value of the enum 'FocusedAphasia' from 5 to 10. Can anyone help me in changing the value of enum at runtime

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

柒夜笙歌凉 2024-12-18 04:25:09

您无法在运行时更改枚举。我不知道为什么你需要这样做,但无论如何这是不可能的。使用变量是另一种选择。

You cannot change enums at runtime. I'm not sure why you'd ever need to, but in any case it's not possible. Using a variable would be the alternative.

玩心态 2024-12-18 04:25:09

你不能这样做,如果你愿意的话,它是一个强类型值

枚举的元素是只读的,并且在运行时更改它们是不可能的,也不是可取的。

可能适合您的是枚举的扩展,以公开与新功能等一起使用的新值,例如:

public enum TestType:int
{
    Aphasia = 2,
    FocusedAphasia = 5
    SomeOtherAphasia = 10
}

不太清楚您想要做什么,我不能很好还有很多其他建议。

You can't do this, it is a strongly-typed value, if you like.

The elements of the enum are read-only and changing them at runtime is not possible, nor would it be desirable.

What might suit you is extension of the enum to expose a new value to use with new features and whatnot, such as:

public enum TestType:int
{
    Aphasia = 2,
    FocusedAphasia = 5
    SomeOtherAphasia = 10
}

Not being quite clued up on exactly what you want to do, I can't well suggest much else.

夏の忆 2024-12-18 04:25:09

好吧,这个问题距我写答案已经七年了。我还是想写一下,也许对以后的人有用。

在运行时更改枚举值是不可能的,但是有一种方法可以通过将 int 变量转换为枚举,并在字典中定义这些整数及其值,如下所示:

// Define enum TestType without values
enum TestType{}
// Define a dictionary for enum values
Dictionary<int,string> d = new Dictionary<int,string>();
void Main()
{
    int i = 5;
    TestType s = (TestType)i;
    TestType e = (TestType)2;

    // Definging enum int values with string values
    d.Add(2,"Aphasia");
    d.Add(5,"FocusedAphasia");

    // Results:
    Console.WriteLine(d[(int)s]); // Result: FocusedAphasia
    Console.WriteLine(d[(int)e]); // Result: Aphasia
}

这样,您就拥有了枚举值的动态字典里面没有任何文字。
如果您想要枚举的任何其他值,那么您可以创建一个方法来添加它:

public void NewEnumValue(int i, string v)
{
    try
    {
        string test = d[i];
        Console.WriteLine("This Key is already assigned with value: " + 
                           test);
    }
    catch
    {
        d.Add(i,v);
    }
}

因此,您最后使用的代码应该是这样的:

// Define enum TestType without values
enum TestType{}
// Define a dictionary for enum values
Dictionary<int,string> d = new Dictionary<int,string>();

public void NewEnumValue(int i, string v)
{
    try
    {
        string test = d[i];
        Console.WriteLine("This Key is already assigned with value: " + 
                           test);
    }
    catch
    {
        d.Add(i,v);
        Console.WriteLine("Addition Done!");
    }
}

void Main()
{
    int i = 5;
    TestType s = (TestType)i;
    TestType e = (TestType)2;

    // Definging enum int values with string values

    NewEnumValue(2,"Aphasia");
    NewEnumValue(5,"FocusedAphasia");
    Console.WriteLine("You will add int with their values; type 0 to " +
                      "exit");
    while(true)
    {
        Console.WriteLine("enum int:");
        int ii = Convert.ToInt32(Console.ReadLine());
        if (ii == 0) break;
        Console.WriteLine("enum value:");
        string v = Console.ReadLine();
        Console.WriteLine("will try to assign the enum TestType with " + 
                          "value: " + v + " by '" + ii + "' int value.");
        NewEnumValue(ii,v);
    }

    // Results:
    Console.WriteLine(d[(int)s]); // Result: FocusedAphasia
    Console.WriteLine(d[(int)e]); // Result: Aphasia
}

Well, this question is 7 years far while I am writing my answer. I still want to write it, maybe it will be useful for someone later.

Changing enum values in runtime is not possible, but there is a way to play around by casting int variable into enum, and defining those ints with their values in a dictionary as in below:

// Define enum TestType without values
enum TestType{}
// Define a dictionary for enum values
Dictionary<int,string> d = new Dictionary<int,string>();
void Main()
{
    int i = 5;
    TestType s = (TestType)i;
    TestType e = (TestType)2;

    // Definging enum int values with string values
    d.Add(2,"Aphasia");
    d.Add(5,"FocusedAphasia");

    // Results:
    Console.WriteLine(d[(int)s]); // Result: FocusedAphasia
    Console.WriteLine(d[(int)e]); // Result: Aphasia
}

In this way, you have a dynamic dictionary for enum values without any written inside it.
In case you want any other value for the enum, then you can make a method to add it:

public void NewEnumValue(int i, string v)
{
    try
    {
        string test = d[i];
        Console.WriteLine("This Key is already assigned with value: " + 
                           test);
    }
    catch
    {
        d.Add(i,v);
    }
}

So, your last code in use should be this way:

// Define enum TestType without values
enum TestType{}
// Define a dictionary for enum values
Dictionary<int,string> d = new Dictionary<int,string>();

public void NewEnumValue(int i, string v)
{
    try
    {
        string test = d[i];
        Console.WriteLine("This Key is already assigned with value: " + 
                           test);
    }
    catch
    {
        d.Add(i,v);
        Console.WriteLine("Addition Done!");
    }
}

void Main()
{
    int i = 5;
    TestType s = (TestType)i;
    TestType e = (TestType)2;

    // Definging enum int values with string values

    NewEnumValue(2,"Aphasia");
    NewEnumValue(5,"FocusedAphasia");
    Console.WriteLine("You will add int with their values; type 0 to " +
                      "exit");
    while(true)
    {
        Console.WriteLine("enum int:");
        int ii = Convert.ToInt32(Console.ReadLine());
        if (ii == 0) break;
        Console.WriteLine("enum value:");
        string v = Console.ReadLine();
        Console.WriteLine("will try to assign the enum TestType with " + 
                          "value: " + v + " by '" + ii + "' int value.");
        NewEnumValue(ii,v);
    }

    // Results:
    Console.WriteLine(d[(int)s]); // Result: FocusedAphasia
    Console.WriteLine(d[(int)e]); // Result: Aphasia
}
陌伤浅笑 2024-12-18 04:25:09

其实你可以。假设您有一个带有原始 TestType 的程序集 (dll)。您可以卸载该程序集(这有点复杂),使用新的 TestType 重写该程序集并重新加载它。

但是,您无法更改现有变量的类型,因为必须在卸载程序集之前处理掉这些变量。

Well actually you can. Say you have an assembly (dll) with the original TestType. You can unload that assembly (this is somewhat complicated), rewrite the assembly with a new TestType and reload it.

You can not, however, change the type of existing variables because those have to be disposed of before the assembly can be unloaded.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文