Solved

I need to reference my object from a T4 template

Posted on 2011-02-18
8
636 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
Monthly Recap

May was a big month for new releases from Linux Academy! Take a look at what our team built recently in our blog. You can access the newest releases from our blog.

 
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

Learn by Doing. Anytime. Anywhere.

Do you like to learn by doing?
Our labs and exercises give you the chance to do just that: Learn by performing actions on real environments.

Hands-on, scenario-based labs give you experience on real environments provided by us so you don't have to worry about breaking anything.

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

695 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