为什么我需要声明类型?

时间:2015-07-27 13:08:29

标签: c# generics

我有以下代码:

public interface IMyActionFactory
{
    AbstractAction<T> CreateAction<T>(MyActionParamBase paramBase = null)
        where T : MyActionParamBase;
}

public sealed class MergeActionParam : MyActionParamBase
{
}

public class MergeTest
{
    private readonly IMyActionFactory _actionFactory = new DefaultMyActionFactory();

    [Theory]
    [PropertyData("MergeWorksData")]
    public void MergeWorks(/*params here*/)
    {
        var param = new MergeActionParam();
        // populate param here

        var sut = _actionFactory.CreateAction<MergeActionParam>(param);
        sut.DoAction();
    }
}

我收到错误

  

“..错误10使用泛型类型'IMyActionFactory'需要1   输入参数......“

为什么编译器期望将类型传递给我的IMyActionFactory,因为我已经声明了没有T的接口?就方法而言,它是唯一期望该类型的方法。我在这里错过了什么吗?

如何在不重新定义界面签名的情况下使其正常工作?

修改 感觉有点尴尬,因为我在一个独立的在线c#编译器中单独放下并快速运行的快速代码不会给出任何编译错误。然而,回到我原来的解决方案(完全数十个项目),错误仍然存​​在。也许与XUnit有关?...不确定

public interface IMyActionFactory
{
    AbstractAction<T> CreateAction<T>(MyActionParamBase paramBase = null)
        where T : MyActionParamBase;
}

public interface IAction
    {
        void DoAction();
    }

 public abstract class AbstractAction<T> : IAction
        where T : MyActionParamBase
    {
        public void DoAction()
        {
        }
}

public class MyActionParamBase
    {

        public MyActionParamBase()
        {
        }
    }

public sealed class MergeActionParam : MyActionParamBase
{
}

public class DefaultMyActionFactory : IMyActionFactory
    {
        public AbstractAction<T> CreateAction<T>(MyActionParamBase paramBase = null) where T : MyActionParamBase
        {
            return null;
        }
    }

public class MergeTest
{
    private readonly IMyActionFactory _actionFactory = new DefaultMyActionFactory();


    public void MergeWorks(/*params here*/)
    {
        var param = new MergeActionParam();
        // populate param here

        var sut = _actionFactory.CreateAction<MergeActionParam>(param);
        sut.DoAction();
    }
}

0 个答案:

没有答案