English 中文(简体)
How do you use .net Reflection with T4?
原标题:

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.

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?

Thanks in advance!

最佳回答

How does one access the current project s contents using T4?

One way is to use the EnvDTE COM component. Googling T4 and EnvDTE should bring back plenty of examples.

Is it possible, and if so, is Reflection available, or is it access to just the raw source that must then be parsed?

Reflection is definitely available from T4. It works mostly as you would expect.

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.

问题回答

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.

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

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:DevelopmentCustomAssembly.dll",newFileName,true);

var assembly = Assembly.LoadFrom(newFileName);
var type = assembly.GetType("CustomAssembly.DummyClass");   
#>
<#=newFileName#>
<#=type#>




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签