简体   繁体   English

C#使用T4生成枚举

[英]C# Using T4 to generate enums

In my project I'd like to use T4 to generate my enums. 在我的项目中,我想使用T4生成我的枚举。 To test this, I created a test project with a simple form. 为了测试这个,我用一个简单的表单创建了一个测试项目。 In this project I added a .tt file and put my code into that file. 在这个项目中,我添加了一个.tt文件并将我的代码放入该文件中。

Everything works, but I was a little bit disappointed. 一切正常,但我有点失望。 I thought that when the project gets executed, the T4 generates the enumeration. 我认为当项目执行时,T4会生成枚举。 So the enumeration is always uptodate. 因此,枚举始终是最新的。 But this ain't the case, right? 但事实并非如此,对吧? If you want the enumeration to be updated, you have to do this manually and rebuild your solution. 如果要更新枚举,则必须手动执行此操作并重建解决方案。 My question is, am I correct in this one? 我的问题是,我在这一个方面是否正确? Or did I miss something. 或者我错过了什么。

Second, why should I use T4 to create an enum? 其次,我为什么要用T4创建一个枚举? I mean if the enum changes I have to rebuild my solution. 我的意思是如果枚举更改我必须重建我的解决方案。

EDIT: I get my enum values from a database table. 编辑:我从数据库表中获取我的枚举值。 The table only has 2 fields: Id and Description 该表只有2个字段:Id和Description

To be honest I don't believe it is worth the effort. 说实话,我不相信这是值得的。 You still have to go back and update your code to handle any new values. 您仍然需要返回并更新代码以处理任何新值。 The best thing you can do is make sure you have a default case for your switch statements. 您可以做的最好的事情是确保您的switch语句有一个默认大小写。

switch (enumValue)
{
    // ...
    default:
        throw new InvalidOperationException(
            "The enum value " + enumValue + " is unhandled."
        );
}

Your table isn't an actual enum equivalent 你的表不是实际的enum

If your table changes it's values it isn't really a good contender to become an enum now is it? 如果你的表改变了它的价值,它现在不是真正成为enum的有力竞争者吗? C# enumerations tend to be statically defined and don't change unless there's a reason for that. C#枚举往往是静态定义的,除非有合理的原因,否则不会改变。 And when they do, code has to adopt to this change to use additional values. 当他们这样做时,代码必须采用此更改来使用其他值。

So if your application is able to change table's content than this is just a usual table. 因此,如果您的应用程序能够更改表的内容,那么这只是一个常用的表。

But in case you want a generic T4 template that actually does generate enumeration with XML documentation and all I've put some effort into writing such a template. 但是如果你想要一个通用的T4模板实际上确实用XML文档生成枚举,那么我已经花了一些精力来编写这样的模板。 Everything is well documented in this blog post . 这篇博客文章中记录了一切。 Including template's code. 包括模板的代码。

Here's an article on going the other direction (generating ac# enum from the SQL view) 这是一篇关于走向另一个方向的文章(从SQL视图生成ac#enum)

http://www.olegsych.com/2008/07/t4-template-for-generating-sql-view-from-csharp-enumeration/ http://www.olegsych.com/2008/07/t4-template-for-generating-sql-view-from-csharp-enumeration/

PLINQO is able to generated enums from one table quite easily but you'd be stuck with LINQ-TO-SQL. PLINQO能够很容易地从一个表生成枚举,但是你会遇到LINQ-TO-SQL。 Good enough if you only need the generated enums though! 如果你只需要生成的枚举就足够了!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM