c#测试类别1';不包含';功能';

c#测试类别1';不包含';功能';,c#,testing,C#,Testing,我对mz测试有问题..Class1'不包含'Function'的定义,并且找不到接受'Class1'类型的第一个参数的扩展方法'Function'(是否缺少using指令或程序集引用?)ClassTestProject2 我在测试库中添加了对我的类Class1的引用,对象也很好这是我的代码我对c#是新手,所以我可能没有做我应该做的事情sobebodz有一些想法吗?谢谢:) 测试: using System; using Microsoft.VisualStudio.TestTools.UnitT

我对mz测试有问题..Class1'不包含'Function'的定义,并且找不到接受'Class1'类型的第一个参数的扩展方法'Function'(是否缺少using指令或程序集引用?)ClassTestProject2

我在测试库中添加了对我的类Class1的引用,对象也很好这是我的代码我对c#是新手,所以我可能没有做我应该做的事情sobebodz有一些想法吗?谢谢:)

测试:

using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using ClassLibrary1;

namespace ClassTestProject2
{
    [TestClass]
    public class UnitTest1
    {
        [TestMethod]
        public void TestMethodPlus()
        {
            double number1 = 9.0;
            double number2 = 1.0;
            string op = "plus";
            double expected = 9.0;
            Class1 count = new Class1();
            double actual = count.Function(number1, number2, op);
            Assert.AreEqual(expected, actual);
        }
   }
}
这是我的班级:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ClassLibrary1
{
    public class Class1
    {
        public static double Function(double num1, double num2, string op)
        {
        double finRes = 0;
        if (op == "plus")
        {
            finRes = num1 + num2;
        }
        else if (op == "minus")
        {
            finRes = num1 - num2;
        }
        else if (op == "multiple")
        {
            finRes = num1 * num2;
        }
        else if (op == "divide")
        {
            finRes = num1 / num2;
        }
        else if (op == "exp")
        {
            finRes = Math.Pow(num1, num2);
        }
        else if (op == "fac")
        {
            double result = num1;
            for (double i = (num1) - 1; i > 0; i--)
            {
                result = result * i;
                finRes = result;
            }
        }
        else if (op == "sqrt")
        {
            finRes = Math.Sqrt(num1);
        }
        return finRes;
    }
}
}

您将
Function
声明为static,这意味着它只能被引用为'Class1.Function(…)`,而不能从Class1的实例引用。

函数是静态的。您不使用实例化的类调用它,而是使用Class1.Function(…)调用它,以防万一:
1.0+9.0!=9.0
那么我该怎么办呢?在这种情况下,可能需要删除
count
变量,只需使用
Class1.Function(…)
。只有当实例具有依赖于此的状态(例如局部变量)和/或行为时,创建实例才变得有用。我做了更正,但它仍然写入错误“Class1”不包含“Function”的定义我只是对所有内容进行了更正,并在我的目录中进行了清理。现在看起来很好,谢谢