【问题标题】:Typewriter: generate variable name with appended postfix打字机:生成带有附加后缀的变量名
【发布时间】:2018-11-06 09:05:14
【问题描述】:

目前我正在使用 Typewriter 从我的 C# 类中自动生成 TypeScript 类:

[TsDTO]
public class MyDto {
    public string Prop1 { get; set; }
    public string Prop2 { get; set; }
}

我想简单地生成一个 Typescript 接口,将后缀“xyz”附加到属性名称:

export Interface IMyDto {
    prop1xyz: string;
    prop2xyz: string;
}

是否可以在不创建自定义方法的情况下定义我的模板以添加“xyz”?

此模板不起作用:

$Classes(c => HasAttribute(c.Attributes, "TsDTO"))[
    export interface I$Name {
        // properties
        $Properties()[$namexyz: $Type;    <-- "xyz" breaks the code!
        ]
    }]

【问题讨论】:

    标签: typewriter


    【解决方案1】:

    在模板顶部的代码块中创建扩展方法:

    ${
        string NameSuffixed(Property p)
        {
            return p.name + "xyz";
        }
    }
    

    然后在你的属性模板中使用它:

    $Classes(c => HasAttribute(c.Attributes, "TsDTO"))[
        export interface I$Name {
            // properties
            $Properties()[$NameSuffixed: $Type;
            ]
        }]
    

    【讨论】:

    • 它不起作用 :-( [$namexyz] 生成字符串“$namexyz”。我需要的是 $name 通常替换为其值并将“xyz”附加到值
    • @GianpieroCaretti,这很奇怪,我以为我曾经让它像那样工作。我用另一个选项更新了我的答案。
    • 我现在看到您在问题中说“没有创建自定义方法”。我不确定这是否可能。
    猜你喜欢
    • 1970-01-01
    • 2011-06-24
    • 1970-01-01
    • 2012-07-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-11
    • 1970-01-01
    相关资源
    最近更新 更多