【问题标题】:Typelite doesn't contain methods in the generated TypeScript fileTypelite 在生成的 TypeScript 文件中不包含方法
【发布时间】:2015-12-19 00:35:36
【问题描述】:

我正在尝试探索 TypeLite 框架,以便能够用 C# 编写代码并随后生成一个 javascript 文件(因为我们在客户端和服务器端都需要相同的代码)。

当我可以将所有类转换为接口时,我取得了初步成功。但是,我坚持转换方法。 TypeLite 是否也支持将方法转换为 Typescript? TypeLite 支持 POCO 对象,因此,理想情况下,我应该能够从这些方法中生成一个 Typescript 方法。

这是我尝试运行的示例代码和我得到的输出:

public class Person
{
    public string Name { get; set; }
    public List<Address> Addresses { get; set; }
    public Dictionary<int, string> dict { get; set; }

    public string GetName()
    {
        return Name + Addresses.ToString();
    }
}

public class Concrete
{
    public Person person { get; set; }
    public string name;

    public Concrete()
    {
        person = new Person();
        name = person.GetName();
    }
}

public class Employee : Person
{
    public decimal Salary { get; set; }
}

这就是我得到的:

declare module MvcApplication4.Controllers {
    interface Person {
        Name: string;
        Addresses: MvcApplication4.Controllers.Address[];
        dict: System.Collections.Generic.KeyValuePair<number, string>[];
    }
    interface Address {
        Street: string;
    }
    interface Concrete {
        person: MvcApplication4.Controllers.Person;
    }
    interface Employee extends MvcApplication4.Controllers.Person {
        Salary: number;
    }
}    
declare module System.Collections.Generic {
    interface KeyValuePair<TKey, TValue> {
        Key: TKey;
        Value: TValue;
     }
}

注意:我在生成的 ts 文件中没有得到 GetName 函数 :(
任何帮助将不胜感激。

【问题讨论】:

    标签: .net typelite


    【解决方案1】:

    TypeLITE 不支持方法,抱歉。 TypeLITE 库的主要目标是帮助您保持跨 .NET / TypeScript 边界的类型一致性,而不是将代码 1:1 从 C# 转换为 TypeScript。

    例如您在 C# 中有一个类,将其序列化并将其作为 JSON 发送到客户端并反序列化它。如果您在 C# 中添加新属性,如果您不在 TypeScript 中添加它,则会收到编译器警告。

    【讨论】:

    • 谢谢卢卡斯。所以我需要搜索一些其他框架,它可以将 C# 方法转换为其相应的 typescript/javascript 等价物:(
    • 您可能对scriptsharp.com 感兴趣——它将 C# 代码编译成 JavaScript
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-05
    • 2013-12-28
    • 1970-01-01
    • 2011-06-12
    • 2013-11-30
    • 1970-01-01
    相关资源
    最近更新 更多