C# 尝试将方法组作为参数传递
使用Microsoft fakes,我在存根对象中具有以下方法签名:C# 尝试将方法组作为参数传递,c#,generics,delegates,stub,microsoft-fakes,C#,Generics,Delegates,Stub,Microsoft Fakes,使用Microsoft fakes,我在存根对象中具有以下方法签名: public void GetAllOf1ExpressionOfFuncOfT0M0Int32Int32<T2>(FakesDelegates.Func<Expression<System.Func<T, T2>>, int, int, IList<T>> stub); public void getAllof1表达式offuncoft0m0int32int32
public void GetAllOf1ExpressionOfFuncOfT0M0Int32Int32<T2>(FakesDelegates.Func<Expression<System.Func<T, T2>>, int, int, IList<T>> stub);
public void getAllof1表达式offuncoft0m0int32int32(fakesdegresents.Func存根);
这是一个实际的存根方法:
IList<T> GetAll<T2>(Expression<Func<T, T2>> orderbyexpr, int nStartAt = -1, int NumToSel = -1);
IList GetAll(表达式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;
}
public static RunReport StubMethod(ref fakesdegresents.Func Func,T returnValue)
{
var counter=newrunreport();
func=()=>
{
联锁增量(参考计数器计数);
返回值;
};
返回计数器;
}
我的问题是我不能理解StubMethod的签名应该是什么以及如何调用它
我尝试了几件事情,导致我“无法分配方法组/无法转换方法组”
顺便说一句-它与其他更简单的存根方法完美配合,如:
IList<T> IRepository<T>.GetAll();
IList IRepository.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;
}
public IList GetAllEntityTypesByName(字符串筛选器)
{
IList types=新列表();
尝试
{
使用(IUnitOfWork uow=ctx.CreateUnitOfWork(true))
{
IRepository repType=uow.CreateRepository();
if(string.IsNullOrEmpty(筛选器))
{
types=repType.GetAll(o=>o.Name);
}
其他的
{
types=repType.Find(t=>t.Name.ToLower().Contains(filter.ToLower()),o=>o.Name);
}
}
}
捕获(例外e)
{
ResourceManager.Instance.Logger.LogException(e);
}
返回类型;
}
另一种看待问题的方式是,我想替换这个:
stubRepType.GetAllOf1ExpressionOfFuncOfT0M0Int32Int32<string>((a, b, c) => { return new List<EntityType>(); });
stubRepType.getAllof1表达式offuncoft0m0int32int32((a,b,c)=>{return new List();});
为此:
TestHelper.StubRepositoryMethod<IList<EntityType>>(ref stubRepType.GetAllOf1ExpressionOfFuncOfT0M0Int32Int32<string>, new List<EntityType>());
TestHelper.StubRepositoryMethod(ref stubRepType.getallof1表达式offuncoft0m0int32int32,new List());
首先尝试使用具体类型。下面是一个工作示例
[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();
}
[TestMethod]
公共void TestMethod1()
{
var stub=新StubIRepository();
stub.getallof1表达式offuncoft0m0int32int32(this.StubGetAll);
var目标=(i假定)存根;
IList datesByMonth=target.GetAll(日期=>date.Month);
Assert.AreEqual(2005年,datesByMonth[0].年);
}
IList StubGetAll(表达式orderByExpr,int startIndex=-1,int numberOfItems=-1)
{
var allDates=new[]{
新日期时间(2001年12月1日),
新日期时间(2002年11月1日),
新日期时间(2003年10月1日),
新日期时间(2004年9月1日),
新日期时间(2005年8月1日)
};
Func orderByFunc=orderByExpr.Compile();
返回allDates.OrderBy(orderByFunc.ToList();
}
解决方案中第七个文件第二行的第四个字符(按第七个字符排序,然后按最后一个编辑日期排序)是邪恶的。有趣的是,如果不在调用StubMethod的代码中添加代码,您的问题几乎是不可能解决的。谢谢,但是有没有办法让它变得通用?而不是为每种类型创建StubGetAll()方法?我想在运行时动态地为func分配新的实现。您介意在伪代码中详细说明您想要的整个场景吗?