Search code examples
c#.netgenericsrhino-mocks

VS2010 compiler error "is not assignable to parameter type T" not a constraint issue I believe


I don't think its a Rhino mocks related.

Is it a compiler bug ?

The line in 2nd block of code below the ERROR: comment is giving the compiler warning, and I dont understand why. What surprised me even more was that block 3 works.

This one works fine, so i converted it to Generic ActivatePresenterAction2

private void ActivatePresenterAction1(IListViewHelper<PairDirEntry> lvh)
{
    var args = lvh.GetArgumentsForCallsMadeOn(
            x => x.ActionOnActivateItem(Arg<Action<PairDirEntry>>.Is.Anything));
    Assert.That(args.Count, Is.EqualTo(1));
    Assert.That(args[0].Length, Is.EqualTo(1));
    var action = (Action<PairDirEntry>)(args[0][0]); // extract the ActivateOnItem action 
    action(_pairDirEntry); // as if ActionOnActivateItem()
}

This one works fails to compile on commented line

private void ActivatePresenterAction2<T>(IListViewHelper<T> lvh) where T : class
{
    var args = lvh.GetArgumentsForCallsMadeOn(
            x => x.ActionOnActivateItem(Arg<Action<T>>.Is.Anything));
    Assert.That(args.Count, Is.EqualTo(1));
    Assert.That(args[0].Length, Is.EqualTo(1));
    var action = (Action<T>)(args[0][0]); // extract the ActivateOnItem action 
    // 
    // ERROR: is not assignable to parameter type T on hliighted line
    // marking the parameter _pairDirEntry
    // 
    action(_pairDirEntry); // as if ActionOnActivateItem()
}

This change to the generic works fine.

ActivatePresenterAction3(_stubSearchResultListViewHelper)(_pairDirEntry);

private Action<T> ActivatePresenterAction3<T>(IListViewHelper<T> lvh) where T : class
{
    var args = lvh.GetArgumentsForCallsMadeOn(
            x => x.ActionOnActivateItem(Arg<Action<T>>.Is.Anything));
    Assert.That(args.Count, Is.EqualTo(1));
    Assert.That(args[0].Length, Is.EqualTo(1));
    return (Action<T>)(args[0][0]);
}

Solution

  • As the compiler error clearly states, _pairDirEntry isn't a T, so you can't pass it to a delegate that takes a T.