Compiler Error CS1061

“type”不包含“member”的定义,并且找不到可接受类型为“type”的第一个参数的扩展方法“name”(是否缺少 using 指令或程序集引用?)。

在尝试调用的方法或访问的类成员不存在时,将发生此错误。

下面的示例生成 CS1061,因为 TestClass1 没有 DisplaySomething 方法。它没有名为 WriteSomething 的方法。该方法可能是此源代码的作者打算编写的方法。

// cs1061.cs
public class TestClass1
{
    // TestClass1 has one method, called WriteSomething.
    public void WriteSomething(string s)
    {
        System.Console.WriteLine(s);
    }
}

public class TestClass2
{
    // TestClass2 has one method, called DisplaySomething.
    public void DisplaySomething(string s)
    {
        System.Console.WriteLine(s);
    }
}

public class TestTheClasses
{
    public static void Main()
    {
        TestClass1 tc1 = new TestClass1();
        TestClass2 tc2 = new TestClass2();
        // The following call fails because TestClass1 does not have 
        // a method called DisplaySomething.
        tc1.DisplaySomething("Hello");      // CS1061

        // To correct the error, change the method call to either 
        // tc1.WriteSomething or tc2.DisplaySomething.
        tc1.WriteSomething("Hello from TestClass1");
        tc2.DisplaySomething("Hello from TestClass2");
    }
}

请参阅

类和结构(C# 编程指南)

扩展方法(C# 编程指南)