【问题标题】:System.Action not known in UnitTest untill explicitly definedSystem.Action 在 UnitTest 中未知,直到明确定义
【发布时间】:2011-03-28 21:59:34
【问题描述】:

我遇到了一个奇怪的问题,即 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 usign VS 2010 sp1 进行编译

* 编辑 2:*

更加简化了测试用例,结果没有变化

* 编辑 3 *

即使在最简单的测试用例中也会失败,请参阅:

[TestMethod()]
public void FromLambdaAsyncTest()
{
    AsyncAction action = c => c();
}

【问题讨论】:

  • 你的代码中没有行号,在哪里WorkflowChannelTest.cs 47 21
  • 无复制,尽可能多地使用此代码。
  • 第 47 行,我现在将尝试在一个新项目中重现它,对不起,不能免费提供所有代码
  • 其实我可以给代码,见:messageworkflow.codeplex.com

标签: c# compiler-errors


【解决方案1】:
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-16
  • 2021-01-04
  • 2023-03-16
相关资源
最近更新 更多