[Mono-dev] Strange Casting bug in .net 4 profile

Gary Thomas Gary.Thomas at ioko.com
Wed Aug 18 07:52:24 EDT 2010


Hi all,
	I have been using mono from trunk and playing with the WCF stuff when I noticed an InvalidCastException being thrown. I figured out that this only happened when using the .net 4 profile, when using .net 3.5 profile it didn't happen.
I have put together a simple app to show the problem and here are the results.

With .net 4:

[mono] ~/Projects/CastBug @ dmcs CastBug.cs /reference:System.Configuration /reference:System.ServiceModel
CastBug.cs(19,41): warning CS0219: The variable `ss' is assigned but its value is never used
Compilation succeeded - 1 warning(s)
[mono] ~/Projects/CastBug @ mono CastBug.exe 
System.ServiceModel.Configuration.ServicesSection
section is ConfigurationSection True
section is ServicesSection False
ServiceSection type System.ServiceModel.Configuration.ServicesSection
Casting section

Unhandled Exception: System.InvalidCastException: Cannot cast from source type to destination type.
  at CastBug.MainClass.Main (System.String[] args) [0x00000] in <filename unknown>:0


With .net 3.5:

[mono] ~/Projects/CastBug @ gmcs CastBug.cs /reference:System.Configuration /reference:System.ServiceModel
CastBug.cs(19,41): warning CS0219: The variable `ss' is assigned but its value is never used
Compilation succeeded - 1 warning(s)
[mono] ~/Projects/CastBug @ mono CastBug.exe 
System.ServiceModel.Configuration.ServicesSection
section is ConfigurationSection True
section is ServicesSection True
ServiceSection type System.ServiceModel.Configuration.ServicesSection
Casting section
Done casting section

The runtime reports the type of the object as ServicesSection but casting it to a ServicesSection causes an InvalidCastException.

Here is the test app:

using System;
using System.Configuration;
using System.ServiceModel.Configuration;

namespace CastBug
{
        class MainClass
        {
                public static void Main (string[] args)
                {
                        var section = ConfigurationManager.GetSection("system.serviceModel/services");
                        Console.WriteLine(section.GetType().ToString());
                        Console.WriteLine("section is ConfigurationSection {0}", section is ConfigurationSection);
                        Console.WriteLine("section is ServicesSection {0}", section is ServicesSection);
                        Console.WriteLine("ServiceSection type {0}", typeof(ServicesSection).ToString());

                        Console.WriteLine("Casting section");
                        // throws an InvalidCastException
                        ServicesSection ss = (ServicesSection)section;
                        Console.WriteLine("Done casting section");

                }
        }
}

Is this a known issue? Should I report a bug? If it was a simple bug in the c# libraries I would have a go at fixing myself but this seem more fundamental than that.

Thanks,
Gary


More information about the Mono-devel-list mailing list