How can I add a field to an Enum? #962
Unanswered
untold-titan
asked this question in
Q&A
Replies: 1 comment
-
Your code should work (however, you might want to add var asmDef = AssemblyDefinition.ReadAssembly(typeof(EItemType).Module.FullyQualifiedName);
var module = asmDef.MainModule;
var itemType = module.GetType(nameof(EItemType));
var TestInjectedItem = new FieldDefinition("InjectedItem", FieldAttributes.Public | FieldAttributes.Static | FieldAttributes.Literal, itemType) { Constant = 114 };
itemType.Fields.Add(TestInjectedItem);
using var stream = new MemoryStream();
asmDef.Write(stream);
var newAsm = Assembly.Load(stream.ToArray());
var newType = newAsm.GetType(nameof(EItemType));
Console.WriteLine("Old type fields");
PrintFields(typeof(EItemType));
Console.WriteLine();
Console.WriteLine("New type fields");
PrintFields(newType);
//--------------------------------------------------------
void PrintFields(Type type)
{
foreach (var field in type.GetFields())
{
Console.WriteLine(field.Name);
}
}
public enum EItemType
{
None = -1,
BasicCardPack = 0
} Code sample above prints the following:
You can run it and see https://dotnetfiddle.net/yBwEvd |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
I'm trying to write a mod for a unity game, and I'm using cecil to edit the main assembly of the game.
This is the code I'm using:
I have double checked using dotPeek to make sure that 114 is a valid constant value (eg incrementing the previous ones)
This is the class I'm editing:
Beta Was this translation helpful? Give feedback.
All reactions