Compiler Error CS1929

'typeB' does not contain a definition for 'method' and the best extension method overload 'typeC.method' requires a receiver of type 'typeA'

This error is generated when you try to invoke an extension method from a class that it does not extend. In the example shown here, the extension method is defined for the derived class D, but not for the base class B.

To correct this error

  1. Create a new extension method for the type where you have to invoke it, or
  2. move the call into an object of the type that the existing method extends.

Example

The following code generates CS1929:

static class Extension
{
    public static void ExtensionMethod(this D d)
    {
    }
}

class D : B
{
}

class B
{
    static void Main()
    {
        B b = new B();
        b.ExtensionMethod(); // CS1929
    }
}

The following code solves the CS1929 as described in 1. - by creating a new extension method for proper type 'B':

static class Extension
{
    public static void ExtensionMethod(this D d)
    {
    }

    public static void NewExtensionMethod(this B b)
    {
    }
}

class D : B
{
}

class B
{
    static void Main()
    {
        B b = new B();
        b.NewExtensionMethod();
    }
}

The following code solves the CS1929 as described in 2. - moving the call into an object of the proper type 'D':

static class Extension
{
    public static void ExtensionMethod(this D d)
    {
    }
}

class D : B
{
}

class B
{
    static void Main()
    {
        D d = new D();
        d.ExtensionMethod();
    }
}

See also