XmlSerializer的是使用泛型类型的限制就是当抛出InvalidOperationException异常的是、抛出、异常、类型

2023-09-04 23:54:58 作者:淡

当我尝试运行下面的code(两个独立的组件)

When I try to run the following code (two separated assemblies)

public interface ITest
{
}

的Program.cs

using System;

public class TestClass
{
    public void Test<T>(T x) where T : ITest { }
}

static class Program
{ 
    static void Main(string[] args)         
    {
        new System.Xml.Serialization.XmlSerializer(typeof(TestClass));
    }
}

编译在Windows 7中使用以下命令64位:

Compiled in Windows 7 64-Bit using the following commands:

C:\ WINDOWS \ Microsoft.NET \框架\ V2.0.50727 \ CSC /目标:图书馆ClassLibrary.cs

c:\Windows\Microsoft.NET\Framework\v2.0.50727\csc /target:library ClassLibrary.cs

C:\ WINDOWS \ Microsoft.NET \框架\ V2.0.50727 \ CSC /reference:ClassLibrary.dll Program.cs的

c:\Windows\Microsoft.NET\Framework\v2.0.50727\csc /reference:ClassLibrary.dll Program.cs

我收到此异常:

System.InvalidOperationException:无法生成临时类   (结果= 1)。错误CS0012:类型   ITEST是在装配定义   未被引用。您必须添加一个   参照装配ClassLibrary,   版本= 0.0.0.0,文化=中立,   公钥=空hinzu。

System.InvalidOperationException: Unable to generate a temporary class (result=1). error CS0012: The type ITest is defined in an assembly that is not referenced. You must add a reference to assembly ClassLibrary, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null hinzu.

在   System.Xml.Serialization.Compiler.Compile(议会   父母,串NS,   XmlSerializerCompilerParameters   xmlParameters,证据证据)   在   System.Xml.Serialization.TempAssembly.GenerateAssembly(XmlMapping []   xmlMappings,类型[]类型,字符串   DefaultNamespace的,证据证据,   XmlSerializerCompilerParameters   参数,装配组装,   哈希表组件)在   System.Xml.Serialization.TempAssembly..ctor(XmlMapping []   xmlMappings,类型[]类型,字符串   DefaultNamespace的,字符串的位置,   证据证据)在   System.Xml.Serialization.XmlSerializer.GenerateTempAssembly(XmlMapping   xmlMapping,类型类型,字符串   DefaultNamespace的)在   System.Xml.Serialization.XmlSerializer..ctor(类型   类型,字符串defaultNamespace)在   Program.Main(字串[] args)

at System.Xml.Serialization.Compiler.Compile(Assembly parent, String ns, XmlSerializerCompilerParameters xmlParameters, Evidence evidence) at System.Xml.Serialization.TempAssembly.GenerateAssembly(XmlMapping[] xmlMappings, Type[] types, String defaultNamespace, Evidence evidence, XmlSerializerCompilerParameters parameters, Assembly assembly, Hashtable assemblies) at System.Xml.Serialization.TempAssembly..ctor(XmlMapping[] xmlMappings, Type[] types, String defaultNamespace, String location, Evidence evidence) at System.Xml.Serialization.XmlSerializer.GenerateTempAssembly(XmlMapping xmlMapping, Type type, String defaultNamespace) at System.Xml.Serialization.XmlSerializer..ctor(Type type, String defaultNamespace) at Program.Main(String[] args)

删除的其中T:ITEST 的距离的的TestClass 的或不使用泛型在所有(如使用的公共无效测试(ITEST X)的)会prevent异常被抛出,但我需要这个结构在​​我的实际应用。

Removing the where T : ITest from TestClass or not using generics at all (e.g. using public void Test(ITest x)) will prevent the exception from being thrown but I need this construct in my real application.

不要任何人明白为什么XmlSerializer的无法处理的限制在哪里?

Do anybody understand why the XmlSerializer is unable to handle the where constraint?

推荐答案

我觉得你是倒霉的。下面是微软对这个问题的反应:

I think you are out of luck. Here's the response from Microsoft about this issue:

感谢您提出这个问题。   不幸的是,我们已经决定,   将不会因为风险寻址   的价值上超过它的好处的修补程序。通过   当时的下一个机会,使   这种变化来一下,希望   该新的串行化   技术的未来版本   Windows通讯基础   将针对您的情况。如果这   问题是造成显著负   对业务的影响,请联系   Microsoft产品支持服务。一世   很遗憾,我们无法提供   更高的分辨率。请放心,   我们认真考虑过这个问题 - 一   不会修复决策从来都不是容易   令。

XmlSerializer 常见问题疑难解答

Thank you for submitting this issue. Unfortunately, we have decided that it will not be addressed because the risk of the fix outweighs its benefit. By the time the next opportunity to make this change comes about, the hope is that the new serialization technologies in a future version of the Windows Communication Foundation will address your scenario. If this issue is causing significant negative business impact, please contact Microsoft Product Support Services. I regret that we could not provide a better resolution. Rest assured that we seriously considered this issue - a Won't Fix decision is never easy to make.

这基本上是说,你应该使用 DataContractSerializer的,而不是 XmlSerializer的或更改对象结构。

This basically says the you should use DataContractSerializer instead of XmlSerializer or change your object structure.