views:

266

answers:

3

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

ClassLibrary.cs

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));
    }
}

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

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

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

I'm getting the this exception:

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.

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)

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.

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

A: 

The type ITest is defined in an assembly that is not referenced. You must add a reference to assembly ClassLibrary

Have you done this?

Webleeuw
take a look at the command I do execute - the argument /reference:ClassLibrary.dll is doing exactly that.
Martin
+1  A: 

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

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.

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

Darin Dimitrov
A: 
Jerry