C# Extension Method Ambiguity: Is the class being extended not part of the signature? -


i having ambiguity problems extension methods. have several similar classes, , each has extension method called "toentity." if more 1 of these extension methods brought scope, compiler doesn't know use, though seems able discern checking extended class trying use it. apparently not case, perhaps i've done wrong? example:

// these classes both have toentity() extension methods return myentity myextendedclass1 model1 = new myextendedclass1(); myextendedclass2 model2 = new myextendedclass2();  myentity myentity1 = model1.toentity(); // ambiguous... why? 

for last few months i've taken shortcut around calling 1 of methods "toentity2()" super lame, i'm adding more classes need have toentity() extension. i'm refactoring , hoping find better way. guess explicit questions are:

why these methods ambiguous instead of being differentiated class extend? options work-around?

update: extension methods defined this:

public static class myextensions {     public static myentity toentity(this myextendedclass1 myextclass)     {         // stuff myextclass         // return myentity     }      public static myentity toentity(this myextendedclass2 myextclass)     {         // stuff myextclass         // return myentity     } } 

is 1 of extension methods defined on interface? this, example, causes ambiguous invocation error:

public static class ext {     public static void a(this icomparable<int> a) { }     public static void a(this valuetype a) { }     public static void callsite() { 1.a(); } } 

the error is:

the call ambiguous between following methods or properties: 'namespace.ext.a(system.icomparable)' , 'namespace.ext.a(system.valuetype)'

if extension methods defined on base classes, however, ambiguity disappears. example:

public static class ext {     public static void a(this object a) { }     public static void a(this valuetype a) { }     public static void callsite() { 1.a(); } } 

Comments

Popular posts from this blog

c++ - OpenCV Error: Assertion failed <scn == 3 ::scn == 4> in unknown function, -

php - render data via PDO::FETCH_FUNC vs loop -

The canvas has been tainted by cross-origin data in chrome only -