Solved

I need to reference my object from a T4 template

Posted on 2011-02-18
8
631 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
8 Comments
 
LVL 3

Expert Comment

by:chandra_darbha
ID: 34931173
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
ID: 34931213
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
ID: 34953296
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 3

Expert Comment

by:chandra_darbha
ID: 34977400
Please post the error you are getting and the T4 code.

Thanks,
Chandra
0
 

Author Comment

by:newbieweb
ID: 34979358
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
ID: 34980418
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
ID: 35007330
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
ID: 35019207
Thanks.
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

763 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