如何实例化 DataReceivedEventArgs 或能够用数据填充它?

发布于 2024-08-03 13:16:52 字数 479 浏览 4 评论 0原文

我正在启动一个进程并重定向错误流,以便能够解析它并知道发生了什么。我这样做是这样的:

_proc.ErrorDataReceived += new DataReceivedEventHandler(NetErrorDataHandler);

其中 NetErrorDataHandler 具有以下签名:

private void NetErrorDataHandler(object sendingProcess, DataReceivedEventArgs errLine)

到目前为止,我必须使用 DataReceivedEventArgs,但我不确定如何测试它。目前我正在运行我正在使用的流程。您无法使用附加数据创建 DataReceivedEventArgs 实例,那么我该如何克服这个障碍呢?我现在看到如何做到这一点的唯一方法是创建一个可以为我完成工作的流程。但是,这并不是用于测试目的的最佳选择。

I'm starting a process and redirecting the error stream to be able to parse it and know what happened. I'm doing it this way:

_proc.ErrorDataReceived += new DataReceivedEventHandler(NetErrorDataHandler);

Where NetErrorDataHandler has the following signature:

private void NetErrorDataHandler(object sendingProcess, DataReceivedEventArgs errLine)

So far I have to work with DataReceivedEventArgs, but I'm not sure how to test it. Currently I'm running the process I'm working with. You can`t create instance of DataReceivedEventArgs with additional data, so how can I overcome this obstacle? The only way I see how to do this now is to create a process that would do the work for me. However, this isn't the best option for testing purposes.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

你是暖光i 2024-08-10 13:16:52

我刚刚发现可以使用内部构造函数创建对象并设置内部字段。显然,这是不鼓励的,但它适用于短期嘲笑。这是一个例子:

    private DataReceivedEventArgs CreateMockDataReceivedEventArgs(string TestData)
    {

        if (String.IsNullOrEmpty(TestData))
            throw new ArgumentException("Data is null or empty.", "Data");

        DataReceivedEventArgs MockEventArgs =
            (DataReceivedEventArgs)System.Runtime.Serialization.FormatterServices
             .GetUninitializedObject(typeof(DataReceivedEventArgs));

        FieldInfo[] EventFields = typeof(DataReceivedEventArgs)
            .GetFields(
                BindingFlags.NonPublic |
                BindingFlags.Instance |
                BindingFlags.DeclaredOnly);

        if (EventFields.Count() > 0)
        {
            EventFields[0].SetValue(MockEventArgs, TestData);
        }
        else
        {
            throw new ApplicationException(
                "Failed to find _data field!");
        }

        return MockEventArgs;

    }

这样使用:

DataReceivedEventArgs TestEventArgs = CreateMockDataReceivedEventArgs("Test");

...这需要一些研究,我认为这是不可能的:-)

I just found out that it is possible to create objects with internal constructors and set the internal fields. Obviously, this is discouraged but it would work for short term mocking. Here's an example:

    private DataReceivedEventArgs CreateMockDataReceivedEventArgs(string TestData)
    {

        if (String.IsNullOrEmpty(TestData))
            throw new ArgumentException("Data is null or empty.", "Data");

        DataReceivedEventArgs MockEventArgs =
            (DataReceivedEventArgs)System.Runtime.Serialization.FormatterServices
             .GetUninitializedObject(typeof(DataReceivedEventArgs));

        FieldInfo[] EventFields = typeof(DataReceivedEventArgs)
            .GetFields(
                BindingFlags.NonPublic |
                BindingFlags.Instance |
                BindingFlags.DeclaredOnly);

        if (EventFields.Count() > 0)
        {
            EventFields[0].SetValue(MockEventArgs, TestData);
        }
        else
        {
            throw new ApplicationException(
                "Failed to find _data field!");
        }

        return MockEventArgs;

    }

Use as such:

DataReceivedEventArgs TestEventArgs = CreateMockDataReceivedEventArgs("Test");

...this took a bit of research, I didn't think it was possible :-)

雾里花 2024-08-10 13:16:52

您的应用程序使用 DataReceivedEventArgs 中的哪些数据?

最好的解决方案可能是使用一个类包装 DataReceivedEventArgs,您可以将数据注入到该类中进行测试,然后将其传递给实现方法,而不是在当前事件处理程序内完成所有工作。

让我告诉你。首先,创建自定义包装类:

internal class CustomDataReceivedEventArgs : EventArgs
{

    public string Data { get; set; }


    public CustomDataReceivedEventArgs(string _Data) 
    {
        Data = Data;
    }

    public CustomDataReceivedEventArgs(DataReceivedEventArgs Source) 
    {
        Data = Source.Data;
    }
}

接下来,将事件处理程序实现移至新方法中:

    private void NetErrorDataHandler_Implementation(object sendingProcess, 
        CustomDataReceivedEventArgs errLine) 
    {
        //Your actual event handling implementation here...
    }

最后,将事件处理程序设置为除了调用新方法外不执行任何操作:

    private void NetErrorDataHandler(object sendingProcess, 
        DataReceivedEventArgs errLine)
    {
        NetErrorDataHandler_Implementation(sendingProcess, 
            new CustomDataReceivedEventArgs(errLine));
    }

现在,您可以通过调用实现来测试您的实现,如下所示:

        NetErrorDataHandler_Implementation(new object(),
            new CustomDataReceivedEventArgs("My Test Data"));

What data does your application use out of DataReceivedEventArgs?

Your best solution may be to wrap DataReceivedEventArgs with a class that you are able to inject data into for testing and then pass that to an implementation method instead of doing all of your work inside of the current event handler.

Let me show you. First, create your custom wrapper class:

internal class CustomDataReceivedEventArgs : EventArgs
{

    public string Data { get; set; }


    public CustomDataReceivedEventArgs(string _Data) 
    {
        Data = Data;
    }

    public CustomDataReceivedEventArgs(DataReceivedEventArgs Source) 
    {
        Data = Source.Data;
    }
}

Next, move your event handler impelmentation into the new method:

    private void NetErrorDataHandler_Implementation(object sendingProcess, 
        CustomDataReceivedEventArgs errLine) 
    {
        //Your actual event handling implementation here...
    }

Finally, set your event handler to do nothing but call the new method:

    private void NetErrorDataHandler(object sendingProcess, 
        DataReceivedEventArgs errLine)
    {
        NetErrorDataHandler_Implementation(sendingProcess, 
            new CustomDataReceivedEventArgs(errLine));
    }

Now, you can test your implementation by calling your implementation like this:

        NetErrorDataHandler_Implementation(new object(),
            new CustomDataReceivedEventArgs("My Test Data"));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文