C#中的动态枚举 [英] Dynamic enum in C#

查看:837
本文介绍了C#中的动态枚举的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何根据数据库查找表中的值(使用企业库数据层)在C#中创建动态枚举(并随后使用枚举选项)?

How do I create a dynamic enum (and subsequently use the enum choices) in C# based on values in a database lookup table (using enterprise library data layer)?

例如,如果我在数据库中添加一个新的查找值,我不想在代码中添加额外的静态枚举值声明。

For example, If I add a new lookup value in the database, I don't want to have to add the extra static enum value declaration in code.

有这样的事情呢?我不想创建代码生成的静态枚举(根据代码项目文章 > 枚举代码生成器 - 从数据库查找表自动生成枚举代码 ),并希望它是完全动态的。

Is there such a thing as this? I don't want to create a code generated static enum (as per The Code Project article Enum Code Generator - Generating enum code automatically from database look up tables) and would prefer it to be completely dynamic.

推荐答案

为此做一些类型的代码生成。

I'm doing this exact thing, but you need to do some kind of code generation for this to work.

在我的解决方案中,我添加了一个项目EnumeratedTypes。这是一个控制台应用程序,从数据库中获取所有的值,并从中构造枚举。

In my solution, I added a project "EnumeratedTypes". This is a console application which gets all of the values from the database and constructs the enums from them. Then it saves all of the enums to an assembly.

枚举生成代码如下:

// 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));

// Get data from database
MyDataAdapter someAdapter = new MyDataAdapter();
MyDataSet.MyDataTable myData = myDataAdapter.GetMyData();

foreach (MyDataSet.MyDataRow row in myData.Rows)
{
    myEnum.DefineLiteral(row.Name, row.Key);
}

// Create the enum
myEnum.CreateType();

// Finally, save the assembly
assemblyBuilder.Save(name.Name + ".dll");

我在解决方案中的其他项目引用此生成的程序集。因此,我可以在代码中使用动态枚举,完成intellisense。

My other projects in the solution reference this generated assembly. As a result, I can then use the dynamic enums in code, complete with intellisense.

然后,我添加了一个后构建事件,以便在这之后EnumeratedTypes项目它会运行自身并生成MyEnums.dll文件。

Then, I added a post-build event so that after this "EnumeratedTypes" project is built, it runs itself and generates the "MyEnums.dll" file.

顺便说一句,它有助于更​​改构建顺序您的项目,以便首先构建EnumeratedTypes。否则,一旦你开始使用动态生成的.dll,如果.dll被删除,你将无法做一个构建。 (鸡和蛋的问题 - 您的其他项目在解决方案需要这个.dll正确建立,你不能创建.dll,直到你建立你的解决方案...)

By the way, it helps to change the build order of your project so that "EnumeratedTypes" is built first. Otherwise, once you start using your dynamically generated .dll, you won't be able to do a build if the .dll ever gets deleted. (Chicken and egg kind of problem -- your other projects in the solution need this .dll to build properly, and you can't create the .dll until you build your solution...)

我得到了上面的大部分代码 this msdn article

I got most of the above code from this msdn article.

希望这有助于!

这篇关于C#中的动态枚举的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆