根据数据库查找表中的值自动创建枚举? [英] Automatically create an Enum based on values in a database lookup table?

查看:442
本文介绍了根据数据库查找表中的值自动创建枚举?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何自动创建枚举,然后基于数据库查找表中的值(使用企业库数据层)在C#中使用其值?

How do I automatically create an enum and subsequently use its values 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 manually add the extra static enum value declaration in code - I'd like to keep the enum in sync with the database.

有这样的事情吗?

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

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

推荐答案

我正在做这件事,但是您需要进行某种代码生成才能使其工作。

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

我从这篇msdn文章

希望这会有所帮助!

这篇关于根据数据库查找表中的值自动创建枚举?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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