C#递归委托声明问题
* 请参阅结尾以进行重要编辑 *
由于各种原因,我有类似的内容:
委托无效任务(QueueTask 队列任务); 委托无效 QueueTask(任务任务);
使用 Visual Studio Express 2008,除了在 lambda 表达式中,它或多或少都可以正常工作,在 lambda 表达式中,如果我尝试在没有明确帮助的情况下在函数中调用和调用queueTask(),它就无法推断出:
void MyTask(QueueTask queueTask) { queueTask(qt => {}); // fails with queueTask doesn't take 1 parameter? }
这虽然有效:但
void MyTask(QueueTask queueTask) { queueTask((QueueTask qt) => {}); }
它不是一个巨大的问题问题,我知道我也可以通过传递另一个包含委托的对象来解决这个问题,但我很好奇是否有一种方法可以让它按原样工作?
谢谢。
* 下面的重要编辑 *
似乎只有当我将类“TaskPool2”放在单独的程序集中时,我才会收到编译错误!?
namespace Test { public class TaskPool2 { public delegate void QueueTask(Task task); public delegate void Task(QueueTask queueTask); public void Queue(Task task) { } } } namespace MyApp { class Program { static void SomeFunc() { Test.TaskPool2 taskPool = new Test.TaskPool2(); taskPool.Queue(queue => { Console.WriteLine("A"); queue(q => Console.WriteLine("C")); }); } } }
确实有点复杂!
* PLEASE SEE END FOR IMPORTANT EDIT *
For various reasons I have something like:
delegate void Task(QueueTask queueTask);
delegate void QueueTask(Task task);
Using Visual Studio Express 2008 this works more or less fine apart from in lambda expressions where it can't deduce if I try call and call queueTask() in the function without explicit help:
void MyTask(QueueTask queueTask) { queueTask(qt => {}); // fails with queueTask doesn't take 1 parameter? }
This works though:
void MyTask(QueueTask queueTask) { queueTask((QueueTask qt) => {}); }
Its not a huge problem and I know I could also probably get away by passing in another object that contained the delegate instead but I was curious if there was a way to make it work as its stands?
Thanks.
* IMPORTANT EDIT BELOW *
It seems only if I put the class 'TaskPool2' in a separate assembly I get the compile error!?
namespace Test { public class TaskPool2 { public delegate void QueueTask(Task task); public delegate void Task(QueueTask queueTask); public void Queue(Task task) { } } } namespace MyApp { class Program { static void SomeFunc() { Test.TaskPool2 taskPool = new Test.TaskPool2(); taskPool.Queue(queue => { Console.WriteLine("A"); queue(q => Console.WriteLine("C")); }); } } }
Granted its somewhat convoluted!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在使用多种方法来破坏\修复代码之后,我发现了一些信息。
当编译器到达该行时,
如果(并且我认为只有)它之前从未遇到过 QueueTask,它将编译失败。
例如,声明类中的下一行(在 SomeFunc 方法之前或之后)
将修复编译。
另外,在 errornouse 行上方添加下一行也可以修复它。
我已经使用 QueueTask 尝试了更多行,并且它们有效(修复了编译)。
我只能假设编译器没有加载 QueueTask 的定义,这导致了这个问题。
看起来甚至像一个错误。
希望我能很好地解释这一点。
After using several ways to break\fix the code, I've found a small bit of information.
When the compiler reaches the line
it'll fall compilation if (and I think only if) it never encountered QueueTask before.
For instance, declaring the next line in the class (before or after the SomeFunc method)
would fix the compilation.
Also, adding the next line above the errornouse line will also fix it
I've tried a few more lines using QueueTask, and they worked (fix the compilation).
I can only assume that the compiler doesn't load the definition for QueueTask, and this leads to this strangment.
Seems even like a bug.
Hope I managed to explain this well.
(这本来是一条注释,但在注释中给出完整的代码实际上并不可行。)
您的代码已经按原样运行:
编译没有问题。
请创建一个类似的简短但完整的程序,无法编译。
(This would have been a comment, but it's not really feasible to give full code in comments.)
Your code already works as it stands:
Compiles with no problems.
Please create a similarly short but complete program which doesn't compile.