我只是通过NuGet将TypeLite安装到我的项目中,但是它不起作用,并且我尝试过的所有操作都没有帮助...这是安装后创建的文件:

<#@ template debug="false" hostspecific="True" language="C#" #>
<#@ assembly name="$(TargetDir)TypeLite.dll" #>
<#@ assembly name="$(TargetDir)TypeLite.Net4.dll" #>
<#@ assembly name="$(TargetDir)$(TargetFileName)" #>

<#@ import namespace="TypeLite" #>
<#@ import namespace="TypeLite.Net4" #>
<#@output extension=".d.ts"#>

 <#@include file="Manager.ttinclude"#>
<# var manager = Manager.Create(Host, GenerationEnvironment); #>

<# var ts = TypeScript.Definitions()
        .WithReference("Enums.ts")
        .ForLoadedAssemblies();
#>

<#= ts.Generate(TsGeneratorOutput.Properties) #>

<# manager.StartNewFile("Enums.ts"); #>
<#= ts.Generate(TsGeneratorOutput.Enums) #>
<# manager.EndBlock(); #>
<# manager.Process(true); #>

这是我得到的错误:
Severity    Code    Description Project File    Line
Error       Running transformation: System.Reflection.ReflectionTypeLoadException: Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information.
   at System.Reflection.RuntimeModule.GetTypes(RuntimeModule module)
   at System.Reflection.RuntimeModule.GetTypes()
   at System.Reflection.Assembly.GetTypes()
   at TypeLite.TsModelBuilder.Add(Assembly assembly)
   at TypeLite.Net4.TypeScriptFluentExtensions.ForLoadedAssemblies(TypeScriptFluent ts)
   at Microsoft.VisualStudio.TextTemplating097C215A707CB07DE5AACDA590F263DF8554A0CCA899B583BF1832F7C0C5D3E595312AE41D3DFD1BB9CA643458AA5D86FE3DBA281ED0000D4B4C1D87EA1DEFFB.GeneratedTextTransformation.TransformText() MyApp   S:\MyApp\Scripts\TypeLite.Net4.tt   1

如果我选择“Debug T4 template”,这就是我得到的:
Severity    Code    Description Project File    Line
Error       Running transformation: System.IO.FileNotFoundException: Could not load file or assembly 'System.Web.Http, Version=5.2.2.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The system cannot find the file specified.
File name: 'System.Web.Http, Version=5.2.2.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'
   at System.ModuleHandle.ResolveType(RuntimeModule module, Int32 typeToken, IntPtr* typeInstArgs, Int32 typeInstCount, IntPtr* methodInstArgs, Int32 methodInstCount, ObjectHandleOnStack type)
   at System.ModuleHandle.ResolveTypeHandleInternal(RuntimeModule module, Int32 typeToken, RuntimeTypeHandle[] typeInstantiationContext, RuntimeTypeHandle[] methodInstantiationContext)
   at System.Reflection.RuntimeModule.ResolveType(Int32 metadataToken, Type[] genericTypeArguments, Type[] genericMethodArguments)
       at System.Reflection.CustomAttribute.FilterCustomAttributeRecord(CustomAttributeRecord caRecord, MetadataImport scope, Assembly& lastAptcaOkAssembly, RuntimeModule decoratedModule, MetadataToken decoratedToken, RuntimeType attributeFilterType, Boolean mustBeInheritable, Object[] attributes, IList derivedAttributes, RuntimeType& attributeType, IRuntimeMethodInfo& ctor, Boolean& ctorHasParameters, Boolean& isVarArg)
   at System.Reflection.CustomAttribute.GetCustomAttributes(RuntimeModule decoratedModule, Int32 decoratedMetadataToken, Int32 pcaCount, RuntimeType attributeFilterType, Boolean mustBeInheritable, IList derivedAttributes, Boolean isDecoratedTargetSecurityTransparent)
   at System.Reflection.CustomAttribute.GetCustomAttributes(RuntimeType type, RuntimeType caType, Boolean inherit)
   at System.RuntimeType.GetCustomAttributes(Type attributeType, Boolean inherit)
   at TypeLite.Extensions.TypeExtensions.GetCustomAttribute[TType](Type type, Boolean inherit)
   at TypeLite.TsModelBuilder.<Add>b__4(Type t)
   at System.Linq.Enumerable.WhereArrayIterator`1.MoveNext()
   at TypeLite.TsModelBuilder.Add(Assembly assembly)
   at TypeLite.Net4.TypeScriptFluentExtensions.ForLoadedAssemblies(TypeScriptFluent ts)
   at Microsoft.VisualStudio.TextTemplating097C215A707CB07DE5AACDA590F263DF8554A0CCA899B583BF1832F7C0C5D3E595312AE41D3DFD1BB9CA643458AA5D86FE3DBA281ED0000D4B4C1D87EA1DEFFB.GeneratedTextTransformation.TransformText() in s:\MyApp\Scripts\TypeLite.Net4.tt:line 13

=== Pre-bind state information ===
LOG: DisplayName = System.Web.Http, Version=5.2.2.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35
 (Fully-specified)
LOG: Appbase = file:///C:/Program Files (x86)/Microsoft Visual Studio 14.0/Common7/IDE/
LOG: Initial PrivatePath = NULL
Calling assembly : Breeze.WebApi2, Version=1.5.0.0, Culture=neutral, PublicKeyToken=f6085f1a45e2ac59.
===
LOG: This bind starts in LoadFrom load context.
WRN: Native image will not be probed in LoadFrom context. Native image will only be probed in default load context, like with Assembly.Load().
LOG: No application configuration file found.
LOG: Using host configuration file:
LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework\v4.0.30319\config\machine.config.
LOG: Post-policy reference: System.Web.Http, Version=5.2.2.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35
LOG: Attempting download of new URL file:///C:/Program Files (x86)/Microsoft Visual Studio 14.0/Common7/IDE/System.Web.Http.DLL.
LOG: Attempting download of new URL file:///C:/Program Files (x86)/Microsoft Visual Studio 14.0/Common7/IDE/System.Web.Http/System.Web.Http.DLL.
LOG: Attempting download of new URL file:///C:/Program Files (x86)/Microsoft Visual Studio 14.0/Common7/IDE/System.Web.Http.EXE.
LOG: Attempting download of new URL file:///C:/Program Files (x86)/Microsoft Visual Studio 14.0/Common7/IDE/System.Web.Http/System.Web.Http.EXE.
LOG: Attempting download of new URL file:///S:/MyApp/bin/System.Web.Http.DLL.
WRN: Comparing the assembly name resulted in the mismatch: Build Number
LOG: Attempting download of new URL file:///S:/MyApp/bin/System.Web.Http/System.Web.Http.DLL.
LOG: Attempting download of new URL file:///S:/MyApp/bin/System.Web.Http.EXE.
LOG: Attempting download of new URL file:///S:/MyApp/bin/System.Web.Http/System.Web.Http.EXE.   MyApp   s:\MyApp\Scripts\TypeLite.Net4.tt   13

如果我将此添加到文件中:



然后我得到:
Error       Compiling transformation: Assembly 'WebUI, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' uses 'System.Web.Http, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' which has a higher version than referenced assembly 'System.Web.Http, Version=5.2.2.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'    MyApp   S:\MyASpp\Scripts\TypeLite.Net4.tt  1

不幸的是,我无法弄清楚那个5.2.2引用在哪里。我项目中的所有内容都是5.2.3。如果我将文件路径移到我的5.2.3 DLL中并将其放入(即nuget中的c:\packages ...),则它将起作用。

我想我的问题是,为什么它不能通过项目找到DLL引用,或者有更好的方法呢?我不需要在此文件中对路径进行硬编码,理想情况下,根本不需要指定路径,但是此时我不知道该怎么做。

最佳答案

得到了同样的错误。就我而言,我希望由typeLite处理的所有文件都在同一程序集中,因此我通过使用以下内容(在.tt文件中)将其限制为一个程序集来解决了这个问题:

<# var ts = TypeScript.Definitions()
    .For(Assembly.GetAssembly(typeof(MyWebProject.SomeClass)));
#>

关于TypeLite全新安装不起作用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34188125/

10-12 04:41