tags:

views:

79

answers:

1

In my project I'd like to use T4 to generate my enums. 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.

Everything works, but I was a little bit disappointed. I thought that when the project gets executed, the T4 generates the enumeration. 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? 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

A: 

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 (enumValue)
{
    // ...
    default:
        throw new InvalidOperationException(
            "The enum value " + enumValue + " is unhandled."
        );
}
ChaosPandion
This is more a workaround, but thanks for the effort though :)
Martijn
@Martijn - Not quite a workaround, more like advice. My point being that using a template to regenerate your enum really doesn't save you much time. You need to manually handle the new values at every place you use the enum.
ChaosPandion