.net - Compiling dynamic code at runtime using T4 and C# -


the articles have read on t4 using texttemplatingfilepreprocessor show how dynamically generate code becomes part of project, , compiled project.

is possible use t4 generate code compiled @ runtime, outputted dll, , loaded , executed, said code having access usual visibility capabilities associated dll?

if so, please point me example.

i'm trying same thing generating dynamic dll using il, rather using c#.

edit

the specific case need straightforward. writing message router routes messages services. services may local or remote. declarative script compiled c#. dynamic part "is service local or remote?". output c# changed accordingly. style of routing different local / remote, hence dynamic nature.

this 1 example of need.

to this, need know 2 things:

  1. you can use run-time t4 template generate text @ runtime, including c# source code.
  2. you can use csharpcodeprovider compile assembly text @ runtime. or manually run csc.exe (the command-line c# compiler) on generated text, more complicated. (actually csharpcodeprovider behind scenes.)

the code this:

var template = new runtimetexttemplate(); string code = template.transformtext();  var compiler = new csharpcodeprovider(); var result = compiler.compileassemblyfromsource(     new compilerparameters { outputassembly = "assembly.dll" }, code); 

Comments

Popular posts from this blog

how to insert data php javascript mysql with multiple array session 2 -

multithreading - Exception in Application constructor -

windows - CertCreateCertificateContext returns CRYPT_E_ASN1_BADTAG / 8009310b -