Mixing common and extension methods

I created an extension method Class1.GetChild<T>() where T : DependencyObjectin the assembly of lib1.dll. After that, all the builds depending on lib1.dll did not compile with an error:

The type "System.Windows.DependencyObject" is defined in the corresponding which is not referenced. You must add a link to the assembly "WindowsBase", etc.

Why do dependent assemblies require WindowsBase, even if they are not using GetChild?

.

Playback (vs2010.net4):

lib1.dll (links to WindowsBase)

namespace lib1
{
    public static class Class1
    {
        public static T GetChild<T>(this DependencyObject src) where T : DependencyObject
        {
            return default(T);
        }
    }

    public static class Class2
    {
        public static int SomeExtMethod(this string src)
        {
            return 0;
        }
    }
}

lib2.dll (links lib1 but not WindowsBase)

using lib1;
class someClass
{
    void someFct()
    {
        "foo".SomeExtMethod(); // error: The type 'System.Windows.DependencyObject'
                // is defined in an assemebly that is not referenced. 
                // You must add a reference to assembly 'WindowsBase' etc..
    }
}

.

Update:

I think something is definitely when mixing common methods and extension methods. I tried to demonstrate the problem in the following example:

// lib0.dll
namespace lib0
{
    public class Class0 { }
}

// lib1.dll
using lib0;
namespace lib1
{
    public static class Class1
    {
        public static void methodA<T>() where T : Class0 { }    // A
        public static void methodB(Class0 e) { }                // B
        public static void methodC(this int src) { }            // C
    }

    public static class Class2
    {
        public static void methodD(this String s) { }
    }
}

// lib2.dll
using lib1;
class someClass
{
    void someFct()
    {
        Class2.methodD("");  // always compile successfully
        "".methodD();        // raise the 'must add reference to lib0' error depending on config. see details below.
    }
}

A, //B, //C → compile ok

A, B, //C → compile ok

//A, B, C → compile ok

A, //B, C → boost error

A, B, C → boost error

//A , methodA . , . .

+5
6

Microsoft : https://connect.microsoft.com/VisualStudio/feedback/details/668498/problem-with-extension-method-in-c-compiler

, , .

:

  • , TP1, , LB1.
  • , LB2.
  • TP1.
  • LB1 TP1

TP1, - , LB1, . , TP1 , LB2 ( ),

+3

, - , . , , , , , .

, WindowsBase.

, prashanth, SomeExtMethod , , , WindowsBase.

: , . , , . , Visual Studio . , ( GAC) / .

: : , , . , ? , , , - , - - , .

? , . , ; .

+2

, , - . , , §7.6.5.1 Invocations , §7.6.5.2. - , , , .

, - ( , ). , - , .

, class, - , .

0

, , , , , , , .

GetChild()

    public static T GetChild<T>(this T src)
    {
        if (typeof(T) == typeof(DependencyObject)) return default(T);
        else return default(T);
    }

- , , WindowsBase, . where T : DependencyObject , .

, , , , - . , , , , , , .

0

, ILMerge . , 2 dll . , dll, . GUI- , .

0

. , . , lib2.dll ( .) .

, , DependencyObject. "WindowsBase".

0

All Articles