Bug 17059 - Generic class doesn't behave the same at runtime when compiled in MonoDevelop and Visual Studio
Summary: Generic class doesn't behave the same at runtime when compiled in MonoDevelop...
Status: RESOLVED FIXED
Alias: None
Product: Compilers
Classification: Mono
Component: C# ()
Version: 3.2.x
Hardware: PC Linux
: --- normal
Target Milestone: ---
Assignee: Marek Safar
URL:
Depends on:
Blocks:
 
Reported: 2014-01-05 02:08 UTC by Struan
Modified: 2014-01-06 05:10 UTC (History)
1 user (show)

Tags:
Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on GitHub or Developer Community with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED FIXED

Description Struan 2014-01-05 02:08:43 UTC
I am seeing different behavior between code compiled in MonoDevelop and Visual Studio. I narrowed it down to this factory class.

    public class Factory<TKey, TBase>
    {
        delegate T InstantiateMethod<T>();
        Dictionary<TKey, InstantiateMethod<TBase>> _Products = new Dictionary<TKey, InstantiateMethod<TBase>>();

        public void Register<T>(TKey key) where T : TBase, new()
        {
            _Products.Add(key, Constructor<T>);
        }
        public TBase Produce(TKey key)
        {
            return _Products[key]();
        }
        static TBase Constructor<T>() where T : TBase, new()
        {
            return new T();
        }
    }

I create a base class and 2 child classes

    class BaseClass
    {
    }
    class ChildClass1 : BaseClass
    {
    }
    class ChildClass2 : BaseClass
    {
    }

then I use the factory class with this code

    var factory = new Factory<byte, BaseClass>();
    factory.Register<ChildClass1>(1);
    factory.Register<ChildClass2>(2);
    Console.WriteLine(factory.Produce(1).GetType());
    Console.WriteLine(factory.Produce(2).GetType());

when compiled in Visual Studio I get the expected output

    MyNameSpace.ChildClass1
    MyNameSpace.ChildClass2

when compiled in MonoDevelop the output is wrong

    MyNameSpace.ChildClass1
    MyNameSpace.ChildClass1

with both cases I was running the .exe on Linux with Mono.
If I register ChildClass2 first then the .exe compiled with MonoDevelop outputs...

    MyNameSpace.ChildClass2
    MyNameSpace.ChildClass2
Comment 1 Marek Safar 2014-01-06 05:10:45 UTC
Fixed in master