C# ServiceStack JSONSerializer和HashSet<;x>;
我们有以下代码:C# ServiceStack JSONSerializer和HashSet<;x>;,c#,asp.net,json,
servicestack,C#,Asp.net,Json,
servicestack,我们有以下代码: // using ServiceStack JSONSerializer string typeInfoString = JsonSerializer.SerializeToString<Type>(typeof(HashSet<string>)); // yields "System.Collections.Generic.HashSet`1[[System.String, mscorlib]], System.Core" // this strin
// using ServiceStack JSONSerializer
string typeInfoString = JsonSerializer.SerializeToString<Type>(typeof(HashSet<string>));
// yields "System.Collections.Generic.HashSet`1[[System.String, mscorlib]], System.Core"
// this string is the same thing, so it's probably valid json
string jsonTypeInfo = typeof(HashSet<string>).ToJson();
// this should work, I feel like
Type desType = JsonSerializer.DeserializeFromString<Type>(jsonTypeInfo);
// but desType ends up being null :(
//使用ServiceStack JSONSerializer
string typeInfoString=JsonSerializer.SerializeToString(typeof(HashSet));
//生成“System.Collections.Generic.HashSet`1[[System.String,mscorlib]],System.Core”
//这个字符串是相同的,所以它可能是有效的json
字符串jsonTypeInfo=typeof(HashSet).ToJson();
//我觉得这应该管用
类型desType=JsonSerializer.DeserializeFromString(jsonTypeInfo);
//但desType最终为空:(
是否有一些ServiceStack获取了有关哈希集类型的信息?这似乎是ServiceStack.Text中的一个错误。我已将问题追踪到AssemblyTypeDefinition.cs第17行(在撰写本文时)。传入的类型定义是“System.Collections.Generic.HashSet`1[[System.String,mscorlib]],System.Core”TypeDefinitionSeparator是“,”导致字符串在“,”的第一个实例而不是应该在第二个实例中被打断。模拟正确拆分的字符串(在调试器中)会从代码中返回正确的结果
您可能希望将其作为bug提交给ServiceStack社区。虽然这不是最好的答案,但您可以通过创建从HashSet继承的本地类来解决这个问题 例如:
public class HashSetHack<T> : HashSet<T> { }
公共类HashSetHack:HashSet{}
然后参考HashSetHack而不是HashSet,它似乎可以工作。谢谢,我会去做的