【发布时间】:2018-07-15 07:56:42
【问题描述】:
总结:
我有一个包含两个项目的简单解决方案,例如ProjA 和 ProjB。在 ProjA 中,我指定了我的自定义 RTConfigure 方法,用于使用 Reinforeced.Typings 工具将 C# 转换为 TypeScript 代码来流畅地配置翻译过程。在 ProjB 中,我定义了用于翻译的 C# ViewModel 类。
问题:
在 ProjB 中,我在三个单独的文件中定义了三个类每个类都属于它们特定的命名空间。通过调用例如使用全局构建器配置时builder.UseModules(true, false) 生成的代码不包括完全限定的命名空间名称,用于引用关键字 extends 之后的类型的翻译类型以及指定为另一个类成员的类型(属于另一个命名空间,不同于使用的成员类型)。
例如:
- ProjB.Ns1.Class1
- ProjB.Ns2.Class2
- ProjB.Ns3.Class3
所有三个类都只定义了两个简单的 props,其中 Class3 稍有不同,它包含一个类型为 Class1 的额外属性。 Class2 继承自 Class1。
示例代码如下:
// Class1.cs
namespace ReinforcedTypings.DifferentNamespaces.Ns1
{
public class Class1
{
public int AnIntegerPropNs1 { get; set; }
public string AStringPropNs1 { get; set; }
}
}
// Class2.cs
using ReinforcedTypings.DifferentNamespaces.Ns1
namespace ReinforcedTypings.DifferentNamespaces.Ns2
{
public class Class2 : Class1
{
public int AnIntegerPropNs2 { get; set; }
public string AStringPropNs2 { get; set; }
}
}
// Class3.cs
using ReinforcedTypings.DifferentNamespaces.Ns1
namespace ReinforcedTypings.DifferentNamespaces.Ns3
{
public class Class3
{
public int AnIntegerPropNs3 { get; set; }
public string AStringPropNs3 { get; set; }
public Class1 AClass1PropNs3 { get; set; }
}
}
configure方法很简单,定义如下:
public static class RTConfig
{
public static void Configure(ConfigurationBuilder builder)
{
var types = typeof(ReinforcedTypings.DifferentNamespaces.Ns1.Class1).Assembly.GetTypes().ToList();
builder.ExportAsClasses(types.Where(x => x.IsClass), c => c.WithAllFields().WithAllMethods().WithAllProperties());
builder.Global(c => c.UseModules(true, false));
// ^ commenting out this line will also cause a different error
}
}
我使用 .xml 配置文件通过将以下选项 (false) 设置为来指定翻译应全部生成一个文件:
<RtDivideTypesAmongFiles>false</RtDivideTypesAmongFiles>
...这会在(.xml 配置文件中设置的已翻译.ts)中生成以下 TypeScript 代码:
export namespace ReinforcedTypings.DifferentNamespaces.Ns3 {
export class Class3
{
public AnIntegerPropNs3: number;
public AStringPropNs3: string;
public AClass1PropNs3: Class1; // <- error here
}
}
export namespace ReinforcedTypings.DifferentNamespaces.Ns2 {
export class Class2 extends Class1 // <- error here
{
public AnIntegerPropNs2: number;
public AStringPropNs2: string;
}
}
export namespace ReinforcedTypings.DifferentNamespaces.Ns1 {
export class Class1
{
public AnIntegerPropNs1: number;
public AStringPropNs1: string;
}
}
请注意Class1 类型在扩展后和Class3 的已翻译属性成员private AClass1PropNs3: Class1; 中缺少其名称的完整命名空间符号。
这会导致 TypeScript 出现问题,因为由于未指定类型的完整(命名空间)路径名而找不到这些类型。
观察到的附带问题:
当不使用 UseModules 时,如果您注释掉该行,则会在翻译后的 TypeScript 代码中发现另一个问题,这是由于翻译代码的顺序,即在 Class3 中找不到 Class1因为它是在声明之前使用的。
结论:
我没有进入 RT 的源代码,但我怀疑 UseModules(true, false) 存在一个问题,其中第二个参数明确声明不要丢弃命名空间,另一个问题是关于输出代码的顺序。无论如何,将第二个参数值设置为 false 会期望始终和任何地方都有所用类型的 FQN。如果我对这个配置属性的使用有误,那么我建议拥有这样的全局配置将非常有益,它将强制对所有/所需类型使用 FQN。
【问题讨论】:
标签: c# typescript code-translation reinforced-typings