Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to cast object of generic type to generic interface C#

I have two interfaces:

public interface IDbModel {}
public interface IDmModel {}

And classes derived from this:

public class DbModel : IDbModel {}
public class DmModel : IDmModel {}
public class Middle { }

Also I have two interfaces with restrictions:

public interface IRule { }
public interface IRule<in TInput, out TOutput> : IRule
    where TInput : IDmModel
    where TOutput : IDbModel
{
    TOutput Apply(TInput elem);
}

And one abstract class derived from this interface:

public abstract class Rule<TDmModel, TMiddle, TDb> : IRule<TDmModel, TDb>
    where TDmModel : IDmModel
    where TDb : IDbModel
{
    private readonly Func<TDmModel, TMiddle> _rule;
    protected Rule(Func<TDmModel, TMiddle> rule) { _rule = rule; }
    protected abstract TDb Apply(TMiddle transformedMessage);
    public TDb Apply(TDmModel elem) { ... }
}

After this I created two classes derived from this abstract class:

public class RuleA : Rule<DmModel, Middle, DbModel>
{
    public RuleA(Func<DmModel, Middle> rule) : base(rule) {}
    protected override DbMode Apply(Middle transformedMessage) { ... }
}

public class RuleB : RuleA
{
    public RuleB() : base((dm) => new Middle()) {}
}

RuleB : RuleA : Rule< DmModel,Middle,DbModel > : IRule< IDmModel,IDbModel > : IRule

And when I try to cast object of RuleB to IRule<IDmModel, IDbModel> occours unhandled exception

Unable to cast object of type 'ParsreCombinators.RuleB' to type 'ParsreCombinators.IRule`2[ParsreCombinators.IDmModel,ParsreCombinators.IDbModel]'.

var ruleB = (IRule<IDmModel, IDbModel>)new RuleB(); // Exception 
IDbModel dbModel = ruleB.Apply(new DmModel());

What wrong with this

To make the example less confusing I simplify it:

EDIT:

After the answers I understood, what is the problem and to make the example less confusing I simplify it:

public interface IDbModel {}
public interface IDmModel {}

public class DbModel : IDbModel {}
public class DmModel : IDmModel {}

public interface IRule<in TInput, out TOutput>
    where TInput : IDmModel
    where TOutput : IDbModel
{
    TOutput Apply(TInput elem);
}

public class RuleA : IRule<DmModel, DbModel>
{
    public DbModel Apply(DmModel elem) { ... }
}

var ruleA = (IRule<IDmModel, IDbModel>)new RuleA(); // Exception
like image 836
giokoguashvili Avatar asked Jul 07 '17 15:07

giokoguashvili


1 Answers

That's a lot of levels of indirection you got there...

Here's the issue:

public abstract class Rule<TDmModel, TMiddle, TDb> : IRule<TDmModel, TDb>
    where TDmModel : IDmModel
    where TDb : IDbModel

public class RuleA : Rule<DmModel, Middle, DbMode>
public class RuleB : RuleA
...
var ruleB = (IRule<IDmModel, IDbModel>)new RuleB();

RuleB implements IRule<DmModel, DbMode>

This cannot be cast to IRule<IDmModel, IDbModel>. C# does not support this type of casting. For the same reason, you cannot do List<object> b = (List<object>)new List<string>(); (Gives "Cannot convert type 'System.Collections.Generic.List<string> to System.Collections.Generic.List<object>.")

This is an issue with covariance.

Here is some more information from Microsoft on the subject: https://docs.microsoft.com/en-us/dotnet/standard/generics/covariance-and-contravariance

like image 124
Chris Berger Avatar answered Sep 24 '22 17:09

Chris Berger