Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 是否可以使typescript ref DTO生成器尊重可为空的属性?_C#_Typescript_<img Src="//i.stack.imgur.com/WM7S8.png" Height="16" Width="18" Alt="" Class="sponsor Tag Img">servicestack - Fatal编程技术网 servicestack,C#,Typescript,servicestack" /> servicestack,C#,Typescript,servicestack" />

C# 是否可以使typescript ref DTO生成器尊重可为空的属性?

C# 是否可以使typescript ref DTO生成器尊重可为空的属性?,c#,typescript,servicestack,C#,Typescript,servicestack,我试图使用ServiceStack中的typescript ref实用程序实现有效的DTO生成。问题是:对于nullable和reference属性,它不会生成默认值定义 DTO有一个C#定义: 公共类数据 { 公共int值{get;set;} 公共int?可选值{get;set;} 公共字符串文本{get;set;} } 生成的typescript DTO如下所示: 导出类数据 { 公共价值:数字; 公共可选值:数字; 公共文本:字符串; 公共构造函数(init?:Partial){(对象作

我试图使用ServiceStack中的
typescript ref
实用程序实现有效的DTO生成。问题是:对于nullable和reference属性,它不会生成默认值定义

DTO有一个C#定义:

公共类数据
{
公共int值{get;set;}
公共int?可选值{get;set;}
公共字符串文本{get;set;}
}
生成的typescript DTO如下所示:

导出类数据
{
公共价值:数字;
公共可选值:数字;
公共文本:字符串;
公共构造函数(init?:Partial){(对象作为任何对象)。赋值(this,init);}
}
这将导致静态检查问题。您将无法将
undefined
null
值(无论选择什么来表示C 35;
null
值)设置为这些属性。由于
Partial
构造函数,可以省略它们,但仍然不方便

此外,TypeScript编译器不会知道这些字段可以有未定义的值——这就是我们将完全丢失对这些DTO的静态检查的地方

我发现
MakePropertiesOptional:True
将使生成的DTO中的每个属性都成为可选的。但这并没有解决我的问题,而是导致了更多的问题。有没有更灵活的方法来解决这个问题

我需要为上面的类生成DTO,以便如下所示:

导出类数据
{
公共价值:数字;
公共可选值?:数字;
公共文本?:字符串;
公共构造函数(init?:Partial){(对象作为任何对象)。赋值(this,init);}
}

我在最新版本中改进了对此的支持

默认实现现在应该为可空属性生成可选的TypeScript属性。因此,默认情况下,它现在将生成:

export class Data
{
    public value: number;
    public optionalValue?: number;
    public text: string;

    public constructor(init?: Partial<Data>) { (Object as any).assign(this, init); }
}
这将生成您想要的:

export class Data
{
    // @Required()
    public value: number;

    public optionalValue?: number;
    public text?: string;

    public constructor(init?: Partial<Data>) { (Object as any).assign(this, init); }
}
或者使用新的
PropertyTypeFilter
可以使每个属性都可以为空,例如:

TypeScriptGenerator.IsPropertyOptional = (generator, type, prop) => false;

TypeScriptGenerator.PropertyTypeFilter = (gen, type, prop) => 
    gen.GetPropertyType(prop, out var isNullable) + "|null";
export class Data
{
    public value: number|null;
    public optionalValue: number|null;
    public text: string|null;

    public constructor(init?: Partial<Data>) { (Object as any).assign(this, init); }
}
这就是您现在设置的配置:

TypeScriptGenerator.UseNullableProperties = true;
将生成每个可为null的属性,例如:

TypeScriptGenerator.IsPropertyOptional = (generator, type, prop) => false;

TypeScriptGenerator.PropertyTypeFilter = (gen, type, prop) => 
    gen.GetPropertyType(prop, out var isNullable) + "|null";
export class Data
{
    public value: number|null;
    public optionalValue: number|null;
    public text: string|null;

    public constructor(init?: Partial<Data>) { (Object as any).assign(this, init); }
}
导出类数据
{
公共值:数字|空;
公共可选值:数字|空;
公共文本:字符串|空;
公共构造函数(init?:Partial){(对象作为任何对象)。赋值(this,init);}
}

谢谢,
IsPropertyOptional
解决了我的问题。只需稍微扩展一下这个答案:
TypeScriptGenerator
ServiceStack
包中的一个类,它在
ServiceStack.NativeTypes.TypeScript
命名空间中定义。它负责设置由web服务生成的元数据,这些元数据将用于生成DTO是否序列化为
未定义
而不是
?或者我们可以设置为生成
| null
联合类型,而不是使用
类型脚本生成器的可选属性。IsPropertyOptional
?@PavelMikheev除了是一个突破性的更改之外,发出
undefined
而不是预期的
null
将是矛盾和不直观的,并导致序列化无效的JSON,这将破坏所有标准JSON序列化程序。我在类似于IsPropertyOptional的中添加了PropertyTypeFilter,您可以选择更改每个属性的属性类型。今天晚些时候将在MyGet上。@PavelMikheev
PropertyTypeFilter
现在是,请注意,如果您需要任何实例生成器API,请使用两个属性筛选器,而不是配置(可在generator.Config上找到)。谢谢。您能否提供一个示例,说明如何使用
PropertyTypeFilter
实现可选属性的
null
而不是
undefined
?我自己也弄不明白。