Solved

I need to reference my object from a T4 template

Posted on 2011-02-18
8
620 Views
Last Modified: 2012-05-11
I can not seem to reference my object, in the same project, from my T4 template.

Please show the line of code, starting with <#@ that I need to import the namespace.

I assume that once I am able to instantiate a single global object, I can reference it anywhere in the T4 template.

newbieweb
0
Comment
Question by:newbieweb
  • 4
  • 4
8 Comments
 
LVL 3

Expert Comment

by:chandra_darbha
Comment Utility
T4 templates are independent and they can't access anything directly from other classes. If you need an object instantiated elsewhere you need to pass that object as an argument to the template. For that you need to define the argument in the template and also add it to the arguments list when invoking the template.

Syntax for declaring argument in T4 -
<#@ property processor="PropertyProcessor" name="ClassName" type="System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" #>

P.S: you need to provide the Fully qualified assembly name in the "type" attribute.

In C# you should pass the arguments as shown below -

var arguments = new Dictionary<string, PropertyData>();
arguments.Add("ClassName", new PropertyData("MyClass", typeof(string)));
string currentDirectory = Directory.GetCurrentDirectory();  
TemplateHost host = new TemplateHost(currentDirectory, arguments);  
host.TemplateFile = Path.Combine(currentDirectory, "MyTemplate.tt");

Thanks,
Chandra
0
 
LVL 3

Accepted Solution

by:
chandra_darbha earned 500 total points
Comment Utility
Incase if you want to instantiate the object within the T4 template itself then you need to import the assembly and the namespace and then use the Class for instantiation.

<#@ assembly name="Assembly.dll" #>
<#@ import namespace="MyNamespace" #>

Thanks,
Chandra
0
 

Author Comment

by:newbieweb
Comment Utility
Actually, I have been trying (unsuccessfully) to get past the assembly command:
<#@ assembly name="Assembly.dll" #>


how do I find the name of the acceptable DLL? It's built in this same solution and I even located the T4 Template in the same folder as the file I need, but nothing's worked.

Please help point out the full assembly name, based on the attached project view.

Thanks,
newbieweb


 T4 Template Namespace Problem
0
 
LVL 3

Expert Comment

by:chandra_darbha
Comment Utility
Please post the error you are getting and the T4 code.

Thanks,
Chandra
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

Author Comment

by:newbieweb
Comment Utility
Compiling transformation: Metadata file 'RDDBWEB2.RDDBWebAdmin.dll' could not be found      

I do not know what to use in place of Assembly.dll.

<#@ assembly name="Assembly.dll" #>
<#@ import namespace="MyNamespace" #>
0
 
LVL 3

Assisted Solution

by:chandra_darbha
chandra_darbha earned 500 total points
Comment Utility
From what I see in the screenshot above and assuming Visual Studio default namespace naming convention below is what I am guessing. Try this. if it doesn't work out then I would need more details.

<#@ assembly name="RDDBWebAdmin.dll" #>
<#@ import namespace="RDDBWebAdmin.Models" #>

Thanks,
Chandra
0
 

Author Comment

by:newbieweb
Comment Utility
RDDBWebAdmin.dll could not be found. I have had this problem endlessly.

Where does this definition fit into this?
<#@ template language="C#" debug="false" hostspecific="true"#>
<#@ include file="EF.Utility.CS.ttinclude"#>
<#@ output extension=".cs"#>

what's the sequence?
0
 

Author Closing Comment

by:newbieweb
Comment Utility
Thanks.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now