【问题标题】:TypeLite: How to convert IEnumerable<T> to any[]TypeLite:如何将 IEnumerable<T> 转换为 any[]
【发布时间】:2015-11-07 17:13:00
【问题描述】:

假设我有以下 C# 类:

[TsClass]
public class Results<T>
{
    public IEnumerable<T> Values { get; set; }
}

我想让typelite生成如下界面:

interface IResults {
    values: any[];
}

(我在类名后附加一个“I”并将类属性小写,效果很好。)

Typelite 目前正在将 IEnumerable 转换为 IT,这显然是我不想要的。 I 被附加,T 是泛型类型。

interface IResults {
    values: IT[];
}

我查看了以下指向 WithConverter 或 WithFormatter 的 SO 问题。 Typelite: Why is Dictionary<string, object> mapped to KeyValuePair and not to any or [index:string]:any 我还查看了 typelite 文档,但他们没有示例,所以我感觉卡住了。 http://type.litesolutions.net/doc

任何帮助将不胜感激!

【问题讨论】:

    标签: c# typescript dto typelite


    【解决方案1】:

    TypeLite 有很多扩展点,但不幸的是,文档并没有涵盖所有这些。为此,您可以使用自己的TsMemberTypeFormatter

    .WithMemberTypeFormatter((tsProperty, memberTypeName) => {
        var asCollection = tsProperty.PropertyType as TypeLite.TsModels.TsCollection;
        var isCollection = asCollection != null;
    
        if(tsProperty.GenericArguments.Count == 1 && 
           tsProperty.GenericArguments[0] is TypeLite.TsModels.TsClass && 
           ((TypeLite.TsModels.TsClass)tsProperty.GenericArguments[0]).Name =="T") {
               memberTypeName = "any";
        }
    
        return memberTypeName + (isCollection ? string.Concat(Enumerable.Repeat("[]", asCollection.Dimension)) : "");
    })
    

    【讨论】:

    • 太好了,谢谢卢卡斯!一般来说,TypeLite 配置感觉有点冗长,但确实有效。
    【解决方案2】:

    我确实得到了这个工作,但这绝对是一个黑客。有趣的是我没有早点想到。

    <#= ts.Generate(TsGeneratorOutput.Properties).Replace("IT[]", "any[]" ) #>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多