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

查看:187
本文介绍了在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)?

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

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.

是否有这样的事吗?我并不想创建一个code生成的静态枚举(按的code项目文章的 枚举code生成 - 生成枚举$ C $自动从数据库查找ç对照表 的),希望preFER它是完全动态的。

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.

推荐答案

我在做这个确切的事情,但你的需求的做一些code一代人为此工作。

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.

枚举代code是这样的:

The enum generation code is like this:

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

我的解决方案中引用其他项目这个生成的汇编。这样一来,我就可以使用动态枚举在code,配有智能感知。

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...)

我得到了最上面的code从这个MSDN文章

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

希望这有助于!

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

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