-1

I already posted this question with a bad sample.

This code should be better.

To avoid confusion I summarised some code:

using System.Collections.Generic;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main()
        {
            IManager<ISpecificEntity> specificManager = new SpecificEntityManager();
            IManager<IAntoherSpecificEntity> anotherSpecificManager = new AnotherSpecificEntityManager();
            Dictionary<string, IManager<IIdentifier>> managers = new Dictionary<string, IManager<IIdentifier>>();
            managers.Add("SpecificManager", (IManager<IIdentifier>)specificManager);
            managers.Add("AnotherSpecificManager", (IManager<IIdentifier>)anotherSpecificManager);

            foreach (var manager in managers.Values)
            {
                IIdentifier entity = manager.Container.GetEntity();
            }
        }
    }

    internal interface IIdentifier
    {
        int Id { get; set; }
    }

    internal interface ISpecificEntity : IIdentifier
    {
        string SpecificValue { get; set; }
    }

    internal class SpecificEntity : ISpecificEntity
    {
        public int Id { get; set; }
        public string SpecificValue { get; set; }
    }


    internal interface IAntoherSpecificEntity : IIdentifier
    {
        string AnotherSpecificValue { get; set; }
    }

    internal class AntoherSpecificEntity : IAntoherSpecificEntity
    {
        public int Id { get; set; }
        public string AnotherSpecificValue { get; set; }
    }

    internal interface IContainer<out TIdentifier> where TIdentifier : IIdentifier
    {
        TIdentifier GetEntity();
    }

    internal interface ISpecificContainer : IContainer<ISpecificEntity>
    {
    }

    internal class SpecificContainer : ISpecificContainer
    {
        public ISpecificEntity GetEntity()
        {
            return new SpecificEntity { SpecificValue = "SpecificValue" };
        }
    }

    internal interface IAnotherSpecificContainer : IContainer<IAntoherSpecificEntity>
    {
    }

    internal class AnotherSpecificContainer : IAnotherSpecificContainer
    {
        public IAntoherSpecificEntity GetEntity()
        {
            return new AntoherSpecificEntity { AnotherSpecificValue = "AnotherSpecificValue" };
        }
    }

    internal interface IManager<TIdentifier> where TIdentifier : IIdentifier
    {
        IContainer<TIdentifier> Container { get; set; }
    }

    internal class SpecificEntityManager : IManager<ISpecificEntity>
    {
        public IContainer<ISpecificEntity> Container { get; set; }
    }

    internal class AnotherSpecificEntityManager : IManager<IAntoherSpecificEntity>
    {
        public IContainer<IAntoherSpecificEntity> Container { get; set; }
    }
}

When I debug the code I get an InvalidCastException in Main() in line 12.

I know that ISpecificEntity implements IIdentifier. But obviously a direct cast from an IManager<ISpecificEntity> into an IManager<IIdentifier> does not work.

I thought working with covariance could do the trick but changing IManager<TIdentifier> into IManager<in TIdentifier> or IManager<out TIdentifier> does not work either.

So, is there a way do cast specificManager into an IManager<IIdentifier>?

Thanks and all the best.

Community
  • 1
  • 1
Palama
  • 53
  • 4
  • 1
    It should tell you which line of Main() is problematic. Could you tell us that please? – Nick Udell Apr 18 '12 at 15:50
  • possible duplicate of [How to implement generic polymorphism in c#?](http://stackoverflow.com/questions/10211072/how-to-implement-generic-polymorphism-in-c) – H H Apr 18 '12 at 15:54
  • Don't repost, edit and improve your questions. – H H Apr 18 '12 at 15:56

2 Answers2

1

First of all you get the idea of generics wrong.

If you Look at Foo its a generic type. Foo and Foo are NEW types, they do not derive from List, neither the types are conneted by means of inheritance. Using generics creates new types, its does not derive!

However what you are looking for is Covariance and Contravariance. This will allow you to create a kind of "generic polymorphism", but for that you need to specify this within your generic definition. Therefore it will only work for very few framework generics out of the box.

class Program
{
    static void Main(string[] args)
    {
        IManager<IIdentifier> f1 = new C1();
        IManager<IIdentifier> f2 = new SpecificEntityManager(); //IManager<ISpecificEntity>
    }
}

interface IIdentifier { }
interface ISpecificEntity : IIdentifier { }
interface IManager<out T> { }

class C1 : IManager<IIdentifier> { }
class SpecificEntityManager : IManager<ISpecificEntity> { }

Here is what YOU have to change:

internal interface IContainer<out TIdentifier> where TIdentifier : IIdentifier 
{ 
    TIdentifier GetEntity(); 
}
internal interface IManager<out TIdentifier> where TIdentifier : IIdentifier 
{
    IContainer<IIdentifier> Container { get; }
}
internal class SpecificEntityManager : IManager<ISpecificEntity>
{
    public IContainer<IIdentifier> Container { get; set; }
}
internal class AnotherSpecificEntityManager : IManager<IAntoherSpecificEntity>
{
    public IContainer<IIdentifier> Container { get; set; }
}
Jaster
  • 8,255
  • 3
  • 34
  • 60
0

Don't you need a non-generic IManager that IManager<TIdentifier> can inherit from. Then IManager<IIdentifer> is an IManager and can be casted?

David Osborne
  • 6,436
  • 1
  • 21
  • 35
  • If there would be generic 'ISuperManager' that IManager would inherit from, how could I access to the generic property Container? – Palama Apr 18 '12 at 15:59
  • Not necessarily. IManager could inherit from an empty IManager that's not generic. The IManager's are IManagers... I think. – David Osborne Apr 18 '12 at 16:02