Typelite不包含生成的TypeScript文件中的方法

时间:2014-12-14 14:18:57

标签: .net typelite

我正在尝试探索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函数:(
任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

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

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