Using T4 code generation, is it possible to access the types defined in the current project?
For example, if I have an interface and I want to delegate its implementation to another class, i.e.
interface IDoSomething {
public void do_something();
}
class DoSomethingImpl : IDoSomething {
public void do_something() {
// implementation...
}
}
class SomeClass : IDoSomething {
IDoSomething m_doSomething = new DoSomethingImpl();
// forward calls to impl object
public void do_something() {
m_doSomething.do_something();
}
}
I would like to automate the call-forwarding in SomeClass
with code generation; is this possible?
While this doesnt solve the locking problems (although ive heard that VS2010 does), you could try copy the dll to a temp location and just use that copied assembly..
<#@ template debug="false" hostspecific="false" language="C#" #>
<#@ output extension=".txt" #>
<#@ import namespace="System.Reflection" #>
<#@ import namespace="System.IO" #>
<#
var newFileName = System.IO.Path.GetTempFileName();
System.IO.File.Copy(@"C:\Development\CustomAssembly.dll",newFileName,true);
var assembly = Assembly.LoadFrom(newFileName);
var type = assembly.GetType("CustomAssembly.DummyClass");
#>
<#=newFileName#>
<#=type#>
来源:https://stackoverflow.com/questions/1153542/t4-code-generation-access-types-in-current-project