In Unity, I create a dynamic enum and I save it into a generated .dll successfully, as shown here: msdn article and here: Dynamic enum in C# I've just added the FlagsAttribute on the dyanmically created enum
But I noticed that the internal field "value__" is private on .NET 2.0. So when I use this dll in another c# application I cannot cast directly enum values to int or getting the exact masked values. Note that in .NET 4.0 and above this "value__" field is public. Note also that if you create in C# a classic public enum in a 2.0 dll this "value__" is public also (in the dll).
So my question is how to make this special field public in Mono .NET 2.0 using the EnumBuilder ?
Here my code to generate the enum:
static bool CreateEnumerators(AppDomain currentDomain, AssemblyName asmName, string enumName, string relativePathName = "")
{
string targetPathName = currentDomain.BaseDirectory + relativePathName;
if (!System.IO.Directory.Exists(targetPathName))
System.IO.Directory.CreateDirectory(targetPathName);
// Create a dynamic assembly in the current application domain,
// and allow it to be executed and saved to disk.
AssemblyBuilder asmBuilder = currentDomain.DefineDynamicAssembly(asmName,
AssemblyBuilderAccess.RunAndSave,
targetPathName);
// Define a dynamic module
// For a single-module assembly, the module has the same name as the assembly.
ModuleBuilder mdlBuilder = asmBuilder.DefineDynamicModule(asmName.Name,
asmName.Name + ".dll");
// Define a public enumeration and an underlying type of Integer.
EnumBuilder enumBuilder = mdlBuilder.DefineEnum(asmName.Name + "." + enumName,
TypeAttributes.Public, typeof(int));
// Get data from database
int key = 1;
foreach (string literalName in enumLiteralNames)
{
enumBuilder.DefineLiteral(literalName, key);
key = key << 1;
}
// set FlagsAttribute
ConstructorInfo cinfo = typeof(FlagsAttribute).GetConstructor(Type.EmptyTypes);
CustomAttributeBuilder flagsAttribute = new CustomAttributeBuilder(cinfo, new object[] { });
enumBuilder.SetCustomAttribute(flagsAttribute);
// Create the enum
Type finished = enumBuilder.CreateType();
// Check if "value__" is private (by default it is from .NET 2.0 to 3.5)
Console.WriteLine("\nCheck special field: \n");
{
FieldInfo fi = finished.GetField("value__", BindingFlags.Instance | BindingFlags.NonPublic);
if (fi != null)
Console.WriteLine("found as private: " + finished.Name + "." + fi.Name + "{" + fi.Attributes + "}");
}
// in .NET 4.0 and above "value__" is part of the public fields
Console.WriteLine("Fields:");
foreach (FieldInfo fi in finished.GetFields())
{
Console.WriteLine(finished.Name + "." + fi.Name + " " + fi.GetType() + " ");
}
// Finally, save the assembly
string assemblyName = asmName.Name + ".dll";
asmBuilder.Save(assemblyName);
Console.WriteLine("\nCreated assembly '" + targetPathName + assemblyName + "'");
return true;
}
Here just a simple use producing error:
MyTypes.MEnum eTypePhysical = MyTypes.MEnum.Physical;
Debug.Log("Value =" + (int)eTypePhysical);
The error:
Internal compiler error. See the console log for more information. output was:error CS0656: The compiler required member `MyTypes.MyEnum.value__' could not be found or is inaccessible
error CS0656: The compiler required member `MyTypes.MyEnum.value__' could not be found or is inaccessible
Any access to the internal values of the enum produce the same error.
I couldn't get any error on Visual Studio using the Microsft .NET 2.0 Framework for generating the dll and use it. But still by inspecting the dll I see the "value__" of the dynamic enum as private which is apparently what causes that error in Unity. This is why I would like to know if it's possible to declare it public using the EnumBuilder interface for .NET 2.0