T4 – > C#class to Knockout View Model in Typescript

我正在寻找某种T4或类似的方式来获取C#类,并为强大类型的线路和视图模型生成数据的接口。

我发现typelite适用于接口,但我正在寻找能够处理视图模型的东西(或者除此之外)

我发现这个项目看起来很棒: JS的代码项目视图模型

但是当然它会生成javascript,并且在打字时看起来非常简单,包括枚举和inheritance等等,并使其与打字稿一起使用

有谁知道这样做的项目?

如果没有,有关如何构建这个的任何提示? 我不是T4专家,即使使用工具使Visual Studio中的T4编辑不那么糟糕,它看起来也很不可思议。

谢谢!

我做了这个T4,以便通过javascript提供我的DTO合同,也许它可以帮助你

<#@ template debug="true" hostSpecific="true" #> <#@ output extension=".js" #> <#@ Assembly Name="System.Core" #> <#@ Assembly Name="System.Windows.Forms" #> <#@ import namespace="System" #> <#@ import namespace="System.IO" #> <#@ import namespace="System.Diagnostics" #> <#@ import namespace="System.Linq" #> <#@ import namespace="System.Collections" #> <#@ import namespace="System.Collections.Generic" #> <#@ import namespace="EnvDTE" #> <#@ include file="..\T4\Automation.ttinclude"#><# var project = VisualStudioHelper.GetProject("MyApp.Core.Contracts"); var contracts = GetSubClasses("MyApp.Core.Contracts.Commands.Command", project) .Concat(GetSubClasses("MyApp.Core.Contracts.Queries.Query", project)); #>(function(MyApp) { function buildContract(contract) { return { type: contract.constructor.type, data: ko.toJSON(contract) }; } var url = "api/commandQuery"; MyApp.cqrs = { sendQuery: function(query, callback) { $.getJSON(url, buildContract(query), callback); }, sendCommand: function(command) { MyApp.utils.post(url, buildContract(command)); } }; <# foreach(var contract in contracts) { #> <# foreach(var part in BuildNameSpace(contract)) { #><#= part #> <# } var properties = GetProperties(contract).Select(p => CamelCased(p.Name)).ToList(); var args = string.Join(", ", properties); #> window.<#= contract.FullName #> = function(<#= args #>) {<# foreach(var property in properties) {#> this.<#= property #> = <#= property #>;<# } #> }; window.<#= contract.FullName #>.type = "<#= contract.FullName #>"; <# } #> })(window.MyApp = window.MyApp || {}); <#+ private static IEnumerable BuildNameSpace(CodeClass @class) { return BuildNameSpace(@class.Namespace.Name.Split('.'), "window", new List()); } private static IEnumerable BuildNameSpace(IEnumerable @namespace, string parent, List parts) { var part = @namespace.FirstOrDefault(); if (part == null) return parts; var current = string.Format("{0}.{1}", parent, part); parts.Add(string.Format("{0} = ({0} || {{}});", current)); return BuildNameSpace(@namespace.Skip(1), current, parts); } public IEnumerable GetSubClasses(string baseClass, Project project) { return VisualStudioHelper .CodeModel .GetAllCodeElementsOfType(project.CodeModel.CodeElements, EnvDTE.vsCMElement.vsCMElementClass, false) .Cast() .Where(c => GetInheritance(c).Any(b => b.FullName == baseClass) && !c.IsAbstract) .ToList(); } public IEnumerable GetInheritance(CodeClass @class) { return GetInheritance(@class, new List()); } public IEnumerable GetInheritance(CodeClass @class, List collection) { foreach(CodeClass @base in @class.Bases) { collection.Add(@base); GetInheritance(@base, collection); } return collection; } public string CamelCased(string pascalCased) { return pascalCased.Substring(0, 1).ToLower() + pascalCased.Substring(1); } public IEnumerable GetProperties(CodeClass @class) { if (@class == null) return new List(); var baseProperties = GetProperties(@class.Bases.Cast().FirstOrDefault()); return baseProperties.Concat(@class .Members .Cast() .Where(ce => ce.Kind == vsCMElement.vsCMElementProperty) .Cast() .Where(p => p.Access == vsCMAccess.vsCMAccessPublic)); } #> 

它输出一个看起来像这样的js

 (function(MyApp) { function buildContract(contract) { return { type: contract.constructor.type, data: ko.toJSON(contract) }; } var url = "api/commandQuery"; MyApp.cqrs = { sendQuery: function(query, callback) { $.getJSON(url, buildContract(query), callback); }, sendCommand: function(command) { MyApp.utils.post(url, buildContract(command)); } }; window.MyApp = (window.MyApp || {}); window.MyApp.Core = (window.MyApp.Core || {}); window.MyApp.Core.Contracts = (window.MyApp.Core.Contracts || {}); window.MyApp.Core.Contracts.Commands = (window.MyApp.Core.Contracts.Commands || {}); window.MyApp.Core.Contracts.Commands.FooCommand = function(bar) { this.bar = bar; }; window.MyApp.Core.Contracts.Commands.FooCommand.type = "MyApp.Core.Contracts.Commands.FooCommand"; })(window.MyApp = window.MyApp || {}); 

请注意,它依赖于有形T4编辑器模板

 <#@ include file="..\T4\Automation.ttinclude"#>