我有一节课,
public class Instance : IResource
{
public Dictionary<string, object> Value { get; set; }
Run Code Online (Sandbox Code Playgroud)
它被映射到
interface Instance {
Value: System.Collections.Generic.KeyValuePair[];
interface KeyValuePair {
Key: any;
Value: any;
}
Run Code Online (Sandbox Code Playgroud)
我原以为是
interface Instance {
Value: any;
Run Code Online (Sandbox Code Playgroud)
要么
interface Instance {
Value: {[index:string]:any};
Run Code Online (Sandbox Code Playgroud)
我怎样才能改变这一代?
另外,如何跳过代中的名称空间?
快速而肮脏的解决方法是使用正则表达式来改变输出:
运用
<#= Regex.Replace( ts.Generate(TsGeneratorOutput.Properties)
, @":\s*System\.Collections\.Generic\.KeyValuePair\<(?<k>[^\,]+),(?<v>[^\,]+)\>\[\];"
, m=>": {[key: "+m.Groups["k"].Value+"]: "+m.Groups["v"].Value+"};"
, RegexOptions.Multiline)
#>
Run Code Online (Sandbox Code Playgroud)
变换字段
myField: System.Collections.Generic.KeyValuePair<string,OtherClass>[];
Run Code Online (Sandbox Code Playgroud)
至
myField: {[key: string]: OtherClass};
Run Code Online (Sandbox Code Playgroud)
集合类型(任何实现 的类型IEnumerable)被转换为数组。Dictionary<>实现IEnumerable<KeyValuePair<>>并因此转换为数组。然后,项目类型将扩展为其完全限定名称 (FQN):System.Collections.Generic.KeyValuePair。
使用类型转换器可以更改类型名称,但不能更改 FQN。所以它只适用于本地类型。对于字典,您无法通过继承来更改项目类型。
您可以创建一个新的字典类型,而不继承自Dictionary<>. 解决此问题的另一种方法是也使用类型格式化程序:
ts.WithConvertor<Dictionary<string,object>>(t => {
// Embed the real type in $
// "System.Collections.Generic.${ [key: string]: any }$[]"
return "${ [key: string]: any }$";
});
ts.WithFormatter((string memberTypeName, bool isMemberCollection) => {
// Extract the content inside $
string[] pieces = memberTypeName.Split('$');
if (pieces.Length == 3) return pieces[1];
// Default behaviour
return memberTypeName + (isMemberCollection ? "[]" : "");
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1379 次 |
| 最近记录: |