C# 引用外部 DLL 中的方法 returns 出错

C# reference to the method in external DLL returns an error

我正在使用 NUnit 测试另一个小组开发的 DLL,但在尝试调用此外部 DLL 中的方法时出现错误,该外部 DLL 在我的测试项目中被设置为引用。错误是:'UT' 是一个命名空间,但像类型一样使用。 我尝试了几种方法,但没有人在工作。我怎样才能正确引用它?

我用这个尝试的方法与我开发的另一个 class 一起工作,当后者在同一个命名空间中时。现在,对于外部 DLL,它只是在编译前显示错误。

这是我的程序:

using NUnit.Framework;

namespace UnitTesting.GettingStarted.Tests
{

    [TestFixture]
    public class TestUT
     {
        [TestCase("A12345", "CII", "10000")]

        public void TestAccessVerification(string psCodeUsager, string psCodeApp, string psCodeFonction)
            {
                UT systemUnderTest = new UT();
                Assert.IsTrue(systemUnderTest.VerifierAcces(psCodeUsager, psCodeApp, psCodeFonction));
            }
     }
}

DLL中的源代码是这样的:

using ...;

namespace GZM
{
    public class UT
    {
        public static bool VerifierAcces(string psCodeUsager, string psCodeApp, string psCodeFonction)
        {
            ... // returns true or false
        {
    {        
{

错误发生在行:

UT systemUnderTest = new UT();

两个 'UT' 都带有下划线并带有错误 'UT' 是一个命名空间,但像类型一样使用。

但是,如果我选择:

var systemUnderTest = new GZM.UT();

错误将发生在下一行并且

systemUnderTest.VerifierAcces

将在消息 "Member 'UT.VerifierAcces(string, string, string) cannot be accessed with an instance reference; qualify it with a type name instead."

下加下划线

正常情况下,我的测试应该可以运行 return 正确,但由于调用过程中的错误,我什至无法启动它。

VerifierAcces是静态方法,不能从具体对象实例中调用。

使用

Assert.IsTrue(UT.VerifierAcces(psCodeUsager, psCodeApp, psCodeFonction));

相反。

您无法访问已实例化的 class 的静态方法。如果不需要将整个 class 作为对象,要么将其设为静态,要么对方法 VerifierAccess.

进行非静态覆盖

假设您需要 UT 是一个具体对象,请参阅 Lennart 的第三个选项的答案,该选项可能对您的用例更有意义。