Error creating XmlSerializer for a type which has a field of other type, defined in a loaded on AssemblyResolve event assembly - by Alexz_nder

Status : 

  Fixed<br /><br />
		This item has been fixed in the current or upcoming version of this product.<br /><br />
		A more detailed explanation for the resolution of this particular item may have been provided in the comments section.


1
0
Sign in
to vote
ID 669551 Comments
Status Closed Workarounds
Type Bug Repros 1
Opened 5/20/2011 1:42:23 AM
Access Restriction Public

Description

When you try to create an XmlSerializer for some type (for example Class1) which has a field of other type (for example Class2) and an assembly, which defines that other type is embedded as a resource in the main assembly and is loaded dynamically on the AssemblyResolve event you get the following exception:

Unable to generate a temporary class (result=1).
error CS0012: The type 'ClassLibrary1.Class2' is defined in an assembly that is not referenced. You must add a reference to assembly 'ClassLibrary1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'.
error CS0266: Cannot implicitly convert type 'object' to 'ClassLibrary1.Class2'. An explicit conversion exists (are you missing a cast?)

Definition for the Class1:

    public class Class1
    {
        public Class2 MyField { get; set; }
    }

Definition for the Class2:

    public class Class2
    {
        public int A { get; set; }
    }

Main method in the program:

        static void Main(string[] args)
        {
            SubscribeAssemblyResolver();
            MainMethod();
        }

        private static void MainMethod()
        {
            XmlSerializer xs = new XmlSerializer(typeof(Class1));            
        }
Sign in to post a comment.
Posted by Jason [MSFT] on 11/9/2011 at 3:03 PM
Hi Per,

This issue is not fixed in 4.0, unfortunately, so downloading .NET Framework 4.0 today would not fix the problem. This does not reproduce under our release under development, which is .NET Framework 4.5.

For experimentation and evaluation in your particular case, the .NET Framework 4.5 Developer Preview is released and available here: http://msdn.microsoft.com/en-us/netframework/hh403373.

This version does not exhibit the problem that Alexz_nder reported.

Please feel free to give the new framework version a spin, however, please do note that installing the developer preview will _replace_ .NET 4.0 with .NET 4.5. Furthermore, this is a pre-beta product and therefore the quality may not be up to what is normally expected of Microsoft products. Therefore, this should be installed only on a non-critical test machine you wouldn't mind reconfiguring.

Finally - while the likelihood of reverting to the 4.0 and 3.5 behaviour is very very low, please note that there is a non-zero probability that changes in a developer preview may not make it into the final release (e.g., if it turns out another part of the framework was destabilized due to this). As such, it may be worth keeping an eye out on the Beta and RC releases and testing against that when they do come out.

Please reply back on this thread if you have further questions. Thanks for using .NET :)

Jason Pang
Software Development Engineer, WCF
Posted by Per Samuelsson on 11/9/2011 at 3:11 AM
"However, at present, the issue does not repro in the next product version under development."

Any chance we could have a more precise answer what this mean? Is it fixed in the current version of 4.0, the version one would download if installing today?

We have a rather major issue with this, and have to maintain an buggy workaround we would like to drop (it would mean we would move our entire product to 4.0 sooner if we knew it was fixed).
Posted by Microsoft on 5/25/2011 at 6:26 PM
Thanks the repro Alexz_nder. We were able in fact able to repro the issue in Visual Studio 2010 and .NET 4.0. However, at present, the issue does not repro in the next product version under development.
Posted by Alexz_nder on 5/25/2011 at 6:22 AM
I forget to mention that I`ve had the same results using VS2008 and .net framework versions 2.0, 3.5 .
Posted by Alexz_nder on 5/25/2011 at 6:17 AM
I see that the issue status is changed to resolved as not reproducable. With the attached solution I`m able to reproduce this bug on 6 different machines running different OS (WinXp, Vista). Can you please attach a solution which the VS development team used trying to reproduce this bug?
Posted by MS-Moderator08 [Feedback Moderator] on 5/20/2011 at 2:19 AM
Thank you for submitting feedback on Visual Studio 2010 and .NET Framework. Your issue has been routed to the appropriate VS development team for review. We will contact you if we require any additional information.