我遇到了这个奇怪的问题,在上面没有明确定义的情况下,System.Action无法解决。为了更好地解释这一点,我向您展示了代码
[TestMethod]
public void Channel_Dispatch_Waits_Before_Return()
{
//Action useless = null;
WorkflowChannel channel = new WorkflowChannel();
bool isHandeled = false;
channel.Subscribe(WorkflowHandlerFactory.FromLambdaAsync<int>((data, callback, onError) =>
{
// Go on another thread and sleep for a small ammount of time to simulate a async request
System.Threading.ThreadPool.QueueUserWorkItem(state =>
{
System.Threading.Thread.Sleep(100);
isHandeled = true;
callback();
});
}));
channel.Dispatch(1);
Assert.IsTrue(isHandeled);
}
这给出了编译时错误,即:
错误1委托'操作'不接受0参数C:\ Users *** \ Documents \ Visual Studio 2010 \ Projects \ MessageWorkflow \ MessageWorkfow.Test \ WorkflowChannelTest.cs 47 21 MessageWorkflow.Test
但是,当我取消注释该行时:
Action useless = null;
它没有给出编译时错误。我试图重建等但没有用。 如果需要,声明:FromLambdaAsync是:
public static IWorkflowHandler FromLambdaAsync<T>(AsyncAction<T> method)
AsyncAction的位置:
public delegate void AsyncAction<T>(T message, Action onComplete, Action<Exception> onError);
它不是一个表现,但我很困惑,因为我无法找到任何背后的逻辑......谁能打我一脸?
干杯!
*编辑*
我设法在最简单的测试用例中重现了这种行为,请参阅:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ClassLibrary2
{
public delegate void AsyncAction(Action onComplete);
public class WorkflowHandler
{
}
public class Main
{
public static WorkflowHandler FromLambdaAsync(AsyncAction method)
{
return new WorkflowHandler();
}
}
}
单元测试:
using ClassLibrary2;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
namespace TestProject1
{
[TestClass]
public class MainTest
{
[TestMethod()]
public void FromLambdaAsyncTest()
{
Main.FromLambdaAsync((callback) =>
{
callback();
});
}
}
}
这给了我同样的错误
该项目正在针对.NET 4.0使用VS 2010 sp1进行编译
*编辑2:*
更简化测试用例,结果没有变化
*编辑3 *
即使在最简单的测试案例中,这也失败了,请参阅:
[TestMethod()]
public void FromLambdaAsyncTest()
{
AsyncAction action = c => c();
}