简体   繁体   中英

Dynamically change the Value of enum in C#?

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

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.

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.

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.

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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM