【问题标题】:Trying to pass method group as parameter尝试将方法组作为参数传递
【发布时间】:2012-11-01 13:34:01
【问题描述】:

使用 Microsoft fakes 我在我的存根对象中有以下方法签名:

 public void GetAllOf1ExpressionOfFuncOfT0M0Int32Int32<T2>(FakesDelegates.Func<Expression<System.Func<T, T2>>, int, int, IList<T>> stub);

这是这个真实的存根方法:

  IList<T> GetAll<T2>(Expression<Func<T, T2>> orderbyexpr, int nStartAt = -1, int NumToSel = -1);

我想做的是通过使用此方法为存根方法分配自定义内容:

  public static RunReport StubMethod<T>(ref FakesDelegates.Func<T> func, T returnValue) 
    {
        var counter = new RunReport();

        func = () =>
                   {
                       Interlocked.Increment(ref counter.Count);
                           return returnValue;
                   };
        return counter;
    }

我的问题是我无法理解 StubMethod 的签名应该是什么以及如何调用它?

我尝试了一些导致“方法组无法分配/无法转换方法组”的事情。

顺便说一句 - 它与其他更简单的存根方法完美配合:

    IList<T> IRepository<T>.GetAll();

所以一定是定义问题...

这是我在代码中使用 GetAll 的地方...这应该重定向到我的自定义函数:

  public IList<EntityType> GetAllEntityTypesByName(string filter)
    {
        IList<EntityType> types = new List<EntityType>();
        try
        {
            using (IUnitOfWork uow = ctx.CreateUnitOfWork(true))
            {
                IRepository<EntityType> repType = uow.CreateRepository<EntityType>();
                if (string.IsNullOrEmpty(filter))
                {
                    types = repType.GetAll(o => o.Name);
                }
                else
                {
                    types = repType.Find(t => t.Name.ToLower().Contains(filter.ToLower()), o => o.Name);
                }
            }
        }
        catch (Exception e)
        {
            ResourceManager.Instance.Logger.LogException(e);
        }
        return types;
    }

另一种看待问题的方式是我想替换这个:

stubRepType.GetAllOf1ExpressionOfFuncOfT0M0Int32Int32<string>((a, b, c) => { return new List<EntityType>(); });

用这个:

TestHelper.StubRepositoryMethod<IList<EntityType>>(ref stubRepType.GetAllOf1ExpressionOfFuncOfT0M0Int32Int32<string>, new List<EntityType>());

【问题讨论】:

  • 解决方案中第七个文件第二行的第四个字符(按第七个字符排序,然后按最后编辑日期排序)是邪恶的。除了有趣之外,如果不查看实际调用 StubMethod 的代码,您的问题几乎是不可能解决的。

标签: c# generics delegates stub microsoft-fakes


【解决方案1】:

首先尝试使用具体类型。这是一个工作示例。

[TestMethod]
public void TestMethod1()
{
    var stub = new StubIRepository<DateTime>();
    stub.GetAllOf1ExpressionOfFuncOfT0M0Int32Int32<int>(this.StubGetAll);
    var target = (IRepository<DateTime>)stub;
    IList<DateTime> datesByMonth = target.GetAll(date => date.Month);
    Assert.AreEqual(2005, datesByMonth[0].Year);
}

IList<DateTime> StubGetAll(Expression<Func<DateTime, int>> orderByExpr, int startIndex = -1, int numberOfItems = -1)
{
    var allDates = new[] {
        new DateTime(2001, 12, 1),
        new DateTime(2002, 11, 1),
        new DateTime(2003, 10, 1),
        new DateTime(2004, 9, 1),
        new DateTime(2005, 8, 1)
    };

    Func<DateTime, int> orderByFunc = orderByExpr.Compile();

    return allDates.OrderBy(orderByFunc).ToList();
}

【讨论】:

  • 谢谢,但有没有办法让它通用?而不是为每种类型创建一个 StubGetAll() 方法?我想在运行时为函数动态分配新的实现。
  • 你介意用伪代码把你想到的场景完整地写出来吗?
猜你喜欢
  • 2020-03-16
  • 1970-01-01
  • 2019-09-14
  • 1970-01-01
  • 2010-11-16
  • 2021-08-22
  • 2018-12-11
  • 1970-01-01
相关资源
最近更新 更多