为什么在DotNet中创建XMLSerializer时,将接口类型成员解释为封闭接口?

发布于 01-23 05:15 字数 1659 浏览 4 评论 0原文

标题说明了一切,考虑到此程序:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文