11

The articles I have read on T4 using TextTemplatingFilePreprocessor show how to dynamically generate code that becomes part of a project, and is compiled with the project.

Is it possible to use T4 to generate code that is compiled at runtime, outputted to a dll, and loaded and executed, with said code having access the usual visibility capabilities associated with a dll?

If so, could you please point me to an example.

I'm effectively trying to do the same thing as generating a dynamic dll using IL, but rather using C#.

EDIT

The specific case I need this for is straightforward. I am writing a message router that routes messages to services. Services may be local or remote. A declarative script is compiled into C#. The dynamic part is "is this service local or remote?". The output C# is changed accordingly. The style of routing is different for local / remote, hence the dynamic nature.

This is one example of what I need.

2
  • I'm not sure you need code generation for that. Can't you have two types: one for local service and one for remote and choose at runtime which one to use? Commented Mar 1, 2013 at 20:29
  • Sure, even a simple if statement that connects the correct delegate. I'm just using this as a simple example of a case where dynamic would benefit. I have more complex cases that I haven't yet fully mapped out. Commented Mar 1, 2013 at 20:36

1 Answer 1

6

To do this, you need to know two things:

  1. You can use run-time T4 template to generate some text at runtime, including C# source code.
  2. You can use CSharpCodeProvider to compile an assembly from text at runtime. Or you could manually run csc.exe (the command-line C# compiler) on the generated text, but that would more complicated. (Actually CSharpCodeProvider does exactly that behind the scenes.)

The code could look like this:

var template = new RuntimeTextTemplate();
string code = template.TransformText();

var compiler = new CSharpCodeProvider();
var result = compiler.CompileAssemblyFromSource(
    new CompilerParameters { OutputAssembly = "assembly.dll" }, code);
Sign up to request clarification or add additional context in comments.

3 Comments

could you point me to something that shows how T4 can evaluate data at runtime (naturally data that determines the output).
@IanC If you create a run-time template, it will be evaluated at runtime (like in my code sample). To modify the input of the template, you can for example use properties in “Class feature control blocks”, which you can then access from the template.
There's a nice post from @ode2code odetocode.com/blogs/scott/archive/2011/01/04/… which shows how to add some simple data in a partial class as well, which is a technique a lot of folks like with runtime templates.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.