What you want to do will not be straight forward.
In order for T4 to use a class (i.e. MyClass) the generator has to reference the containing assembly or a class defined entirely in T4 (this is what the EF code generators do). Furthermore, you are probably using design-time T4 (letting the IDE do the generation). The design time t4 generator will hold a reference to the dll, so you will not be able to build the project again. You will actually have to shut down Visual Studio and restart it. <sigh>
If you decide to proceed with the first approach -- which I highly endorse -- you will want to break your code into two assemblies (3 really). One which will contain code you write, like MyClass. The second for the code that is generated.
Create a third assembly (command line project) and use runtime t4 in here. This project can reference your first assembly (with MyClass) and do anything you want with it, including the code that you have shown here! Have the code generate into the second assembly.
In the project settings of the first project, call the command line tool of the third project as a post-build event. This can also be the pre-build event on the second project.
I have created several successful Aspect Oriented Programming (AOP) solutions with this method. Alas, it may not be the answer you were hoping for.
If you are really brave, you can have the command line tool generate back into the original project. This would require you to compile twice. I cannot recommend this at this time.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…