Q:

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

A:

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

A:

This is a pretty confusing example, but I believe the problem is you're casting to a generic type and giving it interfaces, when the classes your deriving from are forcing you to use DmModel and DbMode. 
Perhaps this is what you meant:
var ruleB = (IRule<DmModel, DbMode>)new RuleB();

That compiles just fine, and I with that way your classes are structured, is the only option other than restructuring.

A:

Your interface IRule<in TInput, out TOutput> is both covariant and contravariant, which means you can't covariantly cast it.  The contravariance prevents this.
Basically, your assignment var dbModel = (IRule<IDmModel, IDbModel>)new RuleB(); asserts that dbModel must accept any IDmModel parameter.  Unfortunately, this is not true; instances must be assignable to DmModel due to the concrete form of RuleA, so other derivatives of IDmModel would fail.

