Using project references as assembly paths in T4

后端 未结 1 1350
旧巷少年郎
旧巷少年郎 2021-02-05 16:30

I have a .tt script that needs to reference a couple of external assemblies.

Is it possible for the T4 host to automatically include the assemblies referenced in the pro

1条回答
  •  生来不讨喜
    2021-02-05 17:01

    i would have posted this in comment if i could.

    for the question: it is not possible to include the assemblies referenced in the project automatically, but you can limit the work you have to do.

    if you see below link in suggestion number 1, you can use c# to define assembly code before it is read by the t4. which make it possible to read a directory with reflection and load each assembly there.so the question is where will your assembly be ?

    List allAssemblies = new List();
    string path = Assembly.GetExecutingAssembly().Location;
    
    foreach (string dll in Directory.GetFiles(path, "*.dll"))
        allAssemblies.Add(Assembly.LoadFile(dll));
        <#@ assembly name=dll #>
    

    this is untested but should get you started at lest. for reference -> how to load all assemblies from within your /bin directory

    for the second part:

    1. using $(SolutionDir) but this is the same as the $(Project) one except one level lower. -> How do I use custom library/project in T4 text template?
    2. use c# path utilities to navigate to the wanted path at runtime, but again this might require the assembly to compile first.
    3. Registering the External Library in GAC. this seams to resolve your problem the most since you will not have to set a path at all. see -> How to register a .Net dll in GAC?

    Edit: here is a working dynamic include. just reference the result of the .ttinclude generated by this in any other .tt file

    i tested it with the debugger and it seems to work.

    and change assembly localisation to point where you need it to.

    <#@ template debug="false" hostspecific="false" language="C#" #>
    <#@ assembly name="System.Core" #>
    <#@ assembly name="System.Net.Http" #>
    <#@ import namespace="System.Linq" #>
    <#@ import namespace="System.Text" #>
    <#@ import namespace="System.Collections.Generic" #>
    <#@ import namespace="System.Reflection" #>
    <#@ import namespace="System.IO" #>
    <#@ output extension=".ttinclude" #><#
    
    List allAssemblies = new List();
    string file = Assembly.GetExecutingAssembly().Location;
    if(file!= "")
    {
        string path = Path.GetDirectoryName(file).TrimEnd();
        if(path != "")
            foreach (string dll in Directory.GetFiles(path, "*.dll"))
            {
                if(dll != "")
                {
                    allAssemblies.Add(Assembly.LoadFile(dll));
                    #>\<#<#= "@ assembly name=\""+ dll +"\" "#>\#><#="\n"#><#
                }
            }
    }
    
    #>
    

    output:

    <#@ assembly name="C:\TEMP\3mo0m0mq.dll" #> 
     <#@ assembly name="C:\TEMP\4ybsqre3.dll" #> 
     <#@ assembly name="C:\TEMP\ao0bzedf.dll" #> 
     <#@ assembly name="C:\TEMP\bo2w102t.dll" #> 
     <#@ assembly name="C:\TEMP\c5o2syvv.dll" #> 
     <#@ assembly name="C:\TEMP\dz1fin10.dll" #> 
     <#@ assembly name="C:\TEMP\giym0gef.dll" #> 
     <#@ assembly name="C:\TEMP\hjfgqkov.dll" #> 
     <#@ assembly name="C:\TEMP\ibuz4wvb.dll" #> 
     <#@ assembly name="C:\TEMP\ilrcwa2y.dll" #> 
     <#@ assembly name="C:\TEMP\k0yeumhb.dll" #> 
     <#@ assembly name="C:\TEMP\kirzdsqp.dll" #> 
     <#@ assembly name="C:\TEMP\ksxl4f2z.dll" #> 
     <#@ assembly name="C:\TEMP\l4kja4ts.dll" #> 
     <#@ assembly name="C:\TEMP\ljgxkpo0.dll" #> 
     <#@ assembly name="C:\TEMP\lkvkmlct.dll" #> 
     <#@ assembly name="C:\TEMP\lnofhhlq.dll" #> 
     <#@ assembly name="C:\TEMP\nbqhmjqd.dll" #> 
     <#@ assembly name="C:\TEMP\oc3pxhmq.dll" #> 
     <#@ assembly name="C:\TEMP\qb43ntcu.dll" #> 
     <#@ assembly name="C:\TEMP\qlyoyhyr.dll" #> 
     <#@ assembly name="C:\TEMP\snwvtb00.dll" #> 
     <#@ assembly name="C:\TEMP\umhhb2wb.dll" #> 
     <#@ assembly name="C:\TEMP\xsyfel0b.dll" #> 
     <#@ assembly name="C:\TEMP\z1weyhko.dll" #> 
    

    you can escape the <# character with \<# see this.

    0 讨论(0)
提交回复
热议问题