views:

272

answers:

1

Hi,

I recently implemented a quick T4 template to generate some data access related classes in our app (Thanks Oleg). One problem I am facing is that my template does need to read data from an XML file that is part of the solution.

Obviously, the path to the XML data must be relative. But when I first implemented it yesterday, it looked like it was starting from the solution root's parent. Today, it wanted to start from the solution root. Though solution root makes more sense, I was wondering if anybody had the official answer.

And maybe someone has a better, more official way of reading data from a file for code-gen with T4.

Regards,

Eric.

+2  A: 

Oleg is still your friend for addressing this problem. See his post Understanding T4: <#@ template #> directive, specifically the section describing the hostspecific parameter.

Setting hostspecific="True" in the template directive results in a Host property being made available to your T4 code. The Host property's type is ITextTemplatingEngineHost, which (amongst other poorly documented things) declares a ResolvePath method that you can use to resolve a path relative to the location of the template file.

Once more, props to Oleg!

Dan Blanchard
Dan, thanks for your reply. My apologies for not thanking you any sooner. I just never got (or noticed) the reply and just now came across it while looking for something else. It does answer my question and I appreciate your time.
Eric Liprandi