关于 C# 中反射的快速问题

发布于 2024-07-16 03:27:51 字数 2092 浏览 4 评论 0原文

我正在开始使用测试驱动开发的概念,并且有点失败,因为我发现我知道测试会是什么样,但我不知道如何让它做我想要的事情。 我拥有的是一个具有 public getter 和 internal setter 的属性。 我想通过从单元测试访问 internal setter 来测试功能,但我不知道如何做到这一点。 这是测试:

    [Test()]
    public void HandleInput() {
        _requestType = _request.GetType();
        PropertyInfo propStdin =
            _requestType.GetProperty("StandardInput", BindingFlags.Public | BindingFlags.NonPublic);
        if(propStdin == null) {
            // Bug in the test.
            throw new Exception("There is a bug in the test. Reflection of stdin property returned null.");
        }
        MethodInfo setStdin = propStdin.GetSetMethod();

        // This will fail at the moment since nothing is here to make this happen.
        Assert.AreEqual("NewInputNewRequestInput", _request.StandardInput);
    }

现在,问题是当我运行测试时,我得到:

[mono-2.4] mbt@zest:~/Projects/StaffASAP/Test.FastCGI/bin/Debug$ nunit-console2 Test.FastCGI.dll
NUnit version 2.4.8
Copyright (C) 2002-2007 Charlie Poole.
Copyright (C) 2002-2004 James W. Newkirk, Michael C. Two, Alexei A. Vorontsov.
Copyright (C) 2000-2002 Philip Craig.
All Rights Reserved.

Runtime Environment - 
   OS Version: Unix 2.6.29.0
  CLR Version: 2.0.50727.1433 ( Mono 2.4 )

..F
Tests run: 2, Failures: 1, Not run: 0, Time: 0.111 seconds

Test Case Failures:
1) Test.FastCGI.tRequest.HandleInput : System.Exception : There is a bug in the test. Reflection of stdin property returned null.
at Test.FastCGI.tRequest.HandleInput () [0x00051] in /home/mbt/Projects/StaffASAP/Test.FastCGI/tRequest.cs:54
at (wrapper managed-to-native) System.Reflection.MonoMethod:InternalInvoke (object,object[],System.Exception&)
at System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00057] in /media/disk/mono-2.4/sources/mono-2.4/mcs/class/corlib/System.Reflection/MonoMethod.cs:159

所以,我必须尝试错误地访问该属性,但从查看文档来看,我不知道我做错了什么。 我做错了什么?

I am getting started with the notion of test-driven development, and kind of failing since I am finding that I know what the test is going to be kind of, but I can't figure out how to get it to do what I want. What I have is a property that has a public getter and an internal setter. I'd like to test the functionality by accessing the internal setter from the unit test, but I can't figure out just how to do it. Here is the test:

    [Test()]
    public void HandleInput() {
        _requestType = _request.GetType();
        PropertyInfo propStdin =
            _requestType.GetProperty("StandardInput", BindingFlags.Public | BindingFlags.NonPublic);
        if(propStdin == null) {
            // Bug in the test.
            throw new Exception("There is a bug in the test. Reflection of stdin property returned null.");
        }
        MethodInfo setStdin = propStdin.GetSetMethod();

        // This will fail at the moment since nothing is here to make this happen.
        Assert.AreEqual("NewInputNewRequestInput", _request.StandardInput);
    }

Now, the problem is that when I run the test, I get:

[mono-2.4] mbt@zest:~/Projects/StaffASAP/Test.FastCGI/bin/Debug$ nunit-console2 Test.FastCGI.dll
NUnit version 2.4.8
Copyright (C) 2002-2007 Charlie Poole.
Copyright (C) 2002-2004 James W. Newkirk, Michael C. Two, Alexei A. Vorontsov.
Copyright (C) 2000-2002 Philip Craig.
All Rights Reserved.

Runtime Environment - 
   OS Version: Unix 2.6.29.0
  CLR Version: 2.0.50727.1433 ( Mono 2.4 )

..F
Tests run: 2, Failures: 1, Not run: 0, Time: 0.111 seconds

Test Case Failures:
1) Test.FastCGI.tRequest.HandleInput : System.Exception : There is a bug in the test. Reflection of stdin property returned null.
at Test.FastCGI.tRequest.HandleInput () [0x00051] in /home/mbt/Projects/StaffASAP/Test.FastCGI/tRequest.cs:54
at (wrapper managed-to-native) System.Reflection.MonoMethod:InternalInvoke (object,object[],System.Exception&)
at System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00057] in /media/disk/mono-2.4/sources/mono-2.4/mcs/class/corlib/System.Reflection/MonoMethod.cs:159

So, I must be attempting to access the property incorrectly, but from looking at the documentation, I don't know what I am doing wrong. What am I doing wrong?

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

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

发布评论

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

评论(3

绿萝 2024-07-23 03:27:51

你失踪了吗| GetProperty(...) 调用中的 BindingFlags.Instance?

不过,通过 InternalsVisibleTo 属性向测试程序公开内部变量会更好,因为您不需要依赖反射,并且如果测试项目是 Visual Studio 中解决方案的一部分,则重构将会传播。

Are you missing | BindingFlags.Instance in the GetProperty(...) call?

It would be much nicer however to expose the internal variables to the test program through the InternalsVisibleTo attribute, as you don't need to rely on reflection, and refactoring will propagate if the test project is part of your solution in Visual Studio.

浮世清欢 2024-07-23 03:27:51

您可能需要查看 InternalsVisibleTo 程序集属性:

[Y]您可以将程序集的内部方法/属性/类公开给“朋友”程序集。 通常,内部结构只能由同一程序集的成员访问,并且通常用于隐藏“管道”方法和实用程序类。

You may want to look at the InternalsVisibleTo assembly attribute:

[Y]ou can expose an assembly's internal methods/properties/classes to a "friend" assembly. Normally internals are only accessible to members of the same assembly, and are often used to hide "plumbing" methods and utilities classes.

你丑哭了我 2024-07-23 03:27:51

这是一篇关于 SO 的类似文章,涉及单元测试、内部成员和反射..

Here is a similar article on SO that deals with Unit tests, internal members and reflection..

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