给梦一个奔跑的方向!
PDF Print E-mail
User Rating: / 0
PoorBest 
Written by xlingfairy
Thursday, 20 May 2010 16:23
t4 : Text Template Transform Toolkit, 文件格式是 tt, 在 VS2010 里有对应的编辑器. VS2008有第三方的插件
 
tt 文件在IDE里可以直接运行, 但是要怎么才能用代码运行呢?
 
You can write a custom host if you want to use the text template transformation functionality from outside Visual Studio or if you want  to integrate that functionality into custom tools. To create a custom host, you must create a class that inherits from  ITextTemplatingEngineHost. For the documentation of the individual methods, see ITextTemplatingEngineHost.
 
意思是说,你需要实现接口 ITextTemplatingEngineHost, 要用这个接口,你需要用
Microsoft.VisualStudio.TextTemplating.VSHost.dll
这个在 VS2008里,是找不到这个DLL的,需要下载:
 
MSDN里有个 ITextTemplatingEngineHost 的例子:
http://msdn.microsoft.com/en-us/library/bb126579.aspx
 
不可以直接使用,需要做一些修改:
1, 必须给类加上 [Serializable]
[Serializable]
    class CustomHost : ITextTemplatingEngineHost {
 
2, 运行的结果可能会是:
ErrorGeneratingOutput
 
这样的话,请检查你的 host.Errors 对象.
一般会提示你 XXX下不存在命名空间XXX
比如:
不存在 System.Linq, 不存在 System.Data.Common, System.Data.SqlClient 等.
 
昨晚我为这个问题搞到了快1点,仍然是带着遗憾入睡的.
我拷贝了 3.5 的 System.Core.dll , System.Linq 存在了, 但是 System.Data.Common, System.Data.SqlClient 这些命名空间仍然找不到,事实上,我也不知道哪个 dll 里有它们.
 
今早又仔细的看了MSDN上的文章,找到了 CustomHost (ITextTemplatingEngineHost) 的属性 StandardAssemblyReferences :
 
The host can provide standard assembly references. The engine will use these references when compiling and executing the generated transformation class.
 
看来,上面的错误是因为没有使用正确的引用路径而引起的.
 
跟据MSDN上的说法,和例子,修改一下 StandardAssemblyReferences 属性:
 
 
//The host can provide standard assembly references.
//The engine will use these references when compiling and
//executing the generated transformation class.
//--------------------------------------------------------------
public IList<string> StandardAssemblyReferences {
    get {
        return new string[]
        {
            //If this host searches standard paths and the GAC,
            //we can specify the assembly name like this.
            //---------------------------------------------------------
            //"System"
 
            //Because this host only resolves assemblies from the 
            //fully qualified path and name of the assembly,
            //this is a quick way to get the code to give us the
            //fully qualified path and name of the System assembly.
            //---------------------------------------------------------
            typeof(System.Uri).Assembly.Location,
            typeof(System.Data.Common.DataAdapter).Assembly.Location,
            typeof(System.Data.SqlClient.SortOrder).Assembly.Location,
            typeof(System.Linq.Queryable).Assembly.Location,
            typeof(System.Web.HttpUtility).Assembly.Location
        };
    }
}
 
 
 
private static string Build() {
    CustomHost host = new CustomHost();//ITextTemplatingEngineHost
    host.TemplateFileValue = "Newsletter.tt";
    Engine engine = new Engine();
    string ctx = engine.ProcessTemplate(File.ReadAllText(host.TemplateFileValue), host);
 
    if (host.Errors.HasErrors) {
        foreach (CompilerError error in host.Errors) {
            Log("Error : " + error.ToString());
        }
        return "";
    }
}
 
这里的 Engine 需要用到Microsoft.VisualStudio.TextTemplating.dll ,  也是从 Visual Studio 2008 SDK 1.1 里找到的.
 
从这里,下载上文所说的两个DLL文件:
http://test.dajiaozi.com/other/Microsoft.VisualStudio.TextTemplating.rar
 

Add comment


Security code
Refresh

Popular Contents

Recommend

Site Info

Members : 1
Content : 140
Web Links : 7
Content View Hits : 111317

Links