0

I am working on a library project. I will distribute this library dll to some other projects for reference. My aim is to generate enum for each time , when ever the project(client project which referenced my dll) build.

I would like create Enum types based on values from api fetch at build time. I am working on .net standard library (2.0) . Requirement is to create a Enum at compile time based on a values, which i will fetch from api call. I tried the following code

 // Get the current application domain for the current thread
        AppDomain currentDomain = AppDomain.CurrentDomain;

        // Create a dynamic assembly in the current application domain,
        // and allow it to be executed and saved to disk.
        AssemblyName name = new AssemblyName("MyEnums");
        AssemblyBuilder assemblyBuilder = currentDomain.DefineDynamicAssembly(name,
                                              AssemblyBuilderAccess.RunAndSave);

        // Define a dynamic module in "MyEnums" assembly.
        // For a single-module assembly, the module has the same name as the assembly.
        ModuleBuilder moduleBuilder = assemblyBuilder.DefineDynamicModule(name.Name,
                                          name.Name + ".dll");

        // Define a public enumeration with the name "MyEnum" and an underlying type of Integer.
        EnumBuilder myEnum = moduleBuilder.DefineEnum("EnumeratedTypes.MyEnum",
                                 TypeAttributes.Public, typeof(int));

but "DefineDynamicAssembly" is not available.

Can anybody guide me to do this. Thanks in advance.

Noorul
  • 873
  • 2
  • 9
  • 26
  • Based on the [documentation by MS](https://learn.microsoft.com/en-us/dotnet/api/system.reflection.emit.assemblybuilder.definedynamicassembly?view=netframework-4.8) the `DefineDynamicAssembly` method is only available starting from .NET Standard 2. – MindSwipe Sep 06 '19 at 06:44
  • If you want to generate code at compile time this wont help you. `Reflection.Emit` generates code at runtime. Maybe just generate some c# lines as strings in a pre compilation step and add them to a template is a better way for you. – thehennyy Sep 06 '19 at 06:44
  • Have a look at [text templates](https://learn.microsoft.com/en-us/visualstudio/modeling/code-generation-and-t4-text-templates). These allow to generate C# code dynamically at build time. – Klaus Gütter Sep 06 '19 at 07:20

1 Answers1

0

Write a Program that writes a .cs file as you need it. In VisualStudio you can Run Commands before and after building a project. For creating Code at compile time, it's the easiest method. Depending on how complex it should be, a simple batch file should be enough but i don't think that api calls are that easy with a batch file.

I think you mean creating a enum at run time, because of the hassle with DefineDynamicAssembly.

JP-Hundhausen
  • 70
  • 1
  • 7
  • hi can you give any sample code to do this, I am working on a library project. I will distribute this library dll to some other projects for reference. My aim is to generate enum for each time , when ever the project(client project which referenced my dll) build.Hope you get clear idea now. – Noorul Sep 12 '19 at 00:56
  • @Noorul It's basically writing a Textfile. [For C#](https://stackoverflow.com/questions/7569904/easiest-way-to-read-from-and-write-to-files) . Write a small C# Program, that creates an entire .cs file with the Enum and so on and add this Program to the pre build Events. What I didn't thought of what changing enums might cause for problems. But with this way you can also create a String that got defined at compile time and not during runtime. – JP-Hundhausen Sep 12 '19 at 14:06