为什么在DotNet中创建XMLSerializer时,将接口类型成员解释为封闭接口?
标题说明了一切,考虑到此程序:
using System;
using System.Xml.Serialization;
public interface ITest
{
public sealed class Test //: ITest
{
public string TestString { get; set; }
}
string TestString { get; }
}
public sealed class Test : ITest
{
public string TestString { get; set; }
}
public class Program
{
public static void Main()
{
var serializer1 = new XmlSerializer(typeof(Test));
Console.WriteLine($"{nameof(serializer1)} created.");
var serializer2 = new XmlSerializer(typeof(ITest.Test));
Console.WriteLine($"{nameof(serializer2)} created.");
}
}
输出是:
serializer1 created. Unhandled exception. System.NotSupportedException: Cannot serialize interface ITest. at System.Xml.Serialization.TypeDesc.CheckSupported() at System.Xml.Serialization.TypeScope.GetTypeDesc(Type type, MemberInfo source, Boolean directReference, Boolean throwOnError) at System.Xml.Serialization.TypeScope.ImportTypeDesc(Type type, MemberInfo memberInfo, Boolean directReference) at System.Xml.Serialization.TypeScope.GetTypeDesc(Type type, MemberInfo source, Boolean directReference, Boolean throwOnError) at System.Xml.Serialization.ModelScope.GetTypeModel(Type type, Boolean directReference) at System.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping(Type type, XmlRootAttribute root, String defaultNamespace) at System.Xml.Serialization.XmlSerializer..ctor(Type type, String defaultNamespace) at System.Xml.Serialization.XmlSerializer..ctor(Type type)
但是itest.test
不是接口,它是(混凝土)密封类!
这是怎么回事?可能是一个错误吗?
The title says it all, consider this program:
using System;
using System.Xml.Serialization;
public interface ITest
{
public sealed class Test //: ITest
{
public string TestString { get; set; }
}
string TestString { get; }
}
public sealed class Test : ITest
{
public string TestString { get; set; }
}
public class Program
{
public static void Main()
{
var serializer1 = new XmlSerializer(typeof(Test));
Console.WriteLine(quot;{nameof(serializer1)} created.");
var serializer2 = new XmlSerializer(typeof(ITest.Test));
Console.WriteLine(quot;{nameof(serializer2)} created.");
}
}
The output is:
serializer1 created. Unhandled exception. System.NotSupportedException: Cannot serialize interface ITest. at System.Xml.Serialization.TypeDesc.CheckSupported() at System.Xml.Serialization.TypeScope.GetTypeDesc(Type type, MemberInfo source, Boolean directReference, Boolean throwOnError) at System.Xml.Serialization.TypeScope.ImportTypeDesc(Type type, MemberInfo memberInfo, Boolean directReference) at System.Xml.Serialization.TypeScope.GetTypeDesc(Type type, MemberInfo source, Boolean directReference, Boolean throwOnError) at System.Xml.Serialization.ModelScope.GetTypeModel(Type type, Boolean directReference) at System.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping(Type type, XmlRootAttribute root, String defaultNamespace) at System.Xml.Serialization.XmlSerializer..ctor(Type type, String defaultNamespace) at System.Xml.Serialization.XmlSerializer..ctor(Type type)
But ITest.Test
is not an interface, it is a (concrete) sealed class!!
What's going on? is it a bug maybe?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论