I have a c# project which includes a Text Template. I would like this template to generate some SQL based on reflecting against the C# classes in the project.
我有一个包含文本模板的c#项目。我希望这个模板基于反映项目中的C#类来生成一些SQL。
How does one access the current project's contents using T4? Is it possible, and if so, is Reflection available, or is it access to just the raw source that must then be parsed?
如何使用T4访问当前项目的内容?是否可能,如果是,可以使用Reflection,还是只访问必须解析的原始源?
Thanks in advance!
提前致谢!
3 个解决方案
#1
4
How does one access the current project's contents using T4?
如何使用T4访问当前项目的内容?
One way is to use the EnvDTE COM component. Googling T4 and EnvDTE should bring back plenty of examples.
一种方法是使用EnvDTE COM组件。谷歌搜索T4和EnvDTE应该带回大量的例子。
Is it possible, and if so, is Reflection available, or is it access to just the raw source that must then be parsed?
是否可能,如果是,可以使用Reflection,还是只访问必须解析的原始源?
Reflection is definitely available from T4. It works mostly as you would expect.
T4绝对可以提供反思。它的工作方式大多与您期望的一样。
Oleg Sych has a number of great blog entries regarding common T4 usage scenarios, but there are plenty of other resources for T4 out there as well.
Oleg Sych有很多关于常见T4使用场景的博客文章,但是T4还有很多其他资源。
#2
4
Completely aside from locking problems, be careful using reflection within a T4 template. The template generator in VS2010 runs against version 4.0 of the Framework, so you could introduce unwanted dependencies if you're generating code for 3.5 or below.
除了锁定问题之外,请小心使用T4模板中的反射。 VS2010中的模板生成器针对Framework 4.0版运行,因此如果您生成3.5或更低版本的代码,则可能会引入不需要的依赖项。
I just found this out the hard way, after using reflection to decide whether to generate parameterless or parameterised calls to ToString
for various BCL types. TimeSpan
has only ToString()
in 2.0, but 4.0 adds ToString(string)
:P
在使用反射决定是否为各种BCL类型生成ToString的无参数或参数化调用之后,我刚刚发现了这个问题。 TimeSpan在2.0中只有ToString(),但是4.0添加了ToString(string):P
#3
0
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..
虽然这不能解决锁定问题(虽然我听说过VS2010会这样做),但您可以尝试将dll复制到临时位置并使用复制的程序集。
<#@ 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#>
#1
4
How does one access the current project's contents using T4?
如何使用T4访问当前项目的内容?
One way is to use the EnvDTE COM component. Googling T4 and EnvDTE should bring back plenty of examples.
一种方法是使用EnvDTE COM组件。谷歌搜索T4和EnvDTE应该带回大量的例子。
Is it possible, and if so, is Reflection available, or is it access to just the raw source that must then be parsed?
是否可能,如果是,可以使用Reflection,还是只访问必须解析的原始源?
Reflection is definitely available from T4. It works mostly as you would expect.
T4绝对可以提供反思。它的工作方式大多与您期望的一样。
Oleg Sych has a number of great blog entries regarding common T4 usage scenarios, but there are plenty of other resources for T4 out there as well.
Oleg Sych有很多关于常见T4使用场景的博客文章,但是T4还有很多其他资源。
#2
4
Completely aside from locking problems, be careful using reflection within a T4 template. The template generator in VS2010 runs against version 4.0 of the Framework, so you could introduce unwanted dependencies if you're generating code for 3.5 or below.
除了锁定问题之外,请小心使用T4模板中的反射。 VS2010中的模板生成器针对Framework 4.0版运行,因此如果您生成3.5或更低版本的代码,则可能会引入不需要的依赖项。
I just found this out the hard way, after using reflection to decide whether to generate parameterless or parameterised calls to ToString
for various BCL types. TimeSpan
has only ToString()
in 2.0, but 4.0 adds ToString(string)
:P
在使用反射决定是否为各种BCL类型生成ToString的无参数或参数化调用之后,我刚刚发现了这个问题。 TimeSpan在2.0中只有ToString(),但是4.0添加了ToString(string):P
#3
0
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..
虽然这不能解决锁定问题(虽然我听说过VS2010会这样做),但您可以尝试将dll复制到临时位置并使用复制的程序集。
<#@ 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#>