Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
689 views
in Technique[技术] by (71.8m points)

visual studio - Can't reference an assembly in a T4 template

I have the following code in a tester class in my main assembly, PocoGenerator. This assembly is supposed to use a T4 template to generate POCO's based on L2S entities in a referenced assembly (a project reference), DataObjects.

var assemblyName = "DataObjects";
var dataObjects = AppDomain.CurrentDomain.Load(new AssemblyName(assemblyName));

Try as I may, I cannot get T4 to find the DataObjects assembly. I have tried various forms of assembly directives, like:

<#@ assembly name="DataObjects" #>
<#@ assembly name="DataObjects, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" #>

to no avail. The code above works in the tester class, but not in the template. What am I doing wrong?

ADDED: I have resolved this issue by using the absolute path to the assembly in bot places I reference it, the directive as well as the class feature block, i.e.

<#@ assembly name="C:DevelopmentPocoGeneratorDataObjectsinDebugDataObjects.dll" #>

and

var sourceAssembly = Assembly.LoadFile(@"C:DevelopmentPocoGeneratorDataObjectsinDebugDataObjects.dll");

But I really don't like this, as I would like to use this template in various projects, and I just plain hate duplication, especially of magic strings.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
<#@ assembly name="$(ProjectDir)binDebugProofOfConcept.dll" #>

Happy Coding!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...