Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ambiguous extension method

Tags:

c#

.net

I am making the following call to an extension method:

database.ExecuteScalar(command).NoNull<string>(string.Empty); 

I get an error that the extension method is ambiguous .

I have two dlls with the same code that implement NoNull(string str) under different namespaces.

How can I explicitly refer to one namespace?

How would I have it done if it was the same namespace?

Update: I cannot rewrite the 3rd party dlls.

like image 925
Elad Benda Avatar asked Dec 12 '11 13:12

Elad Benda


People also ask

How can the ambiguous extension method be resolved?

Ambiguity can be resolved if concurrent namespaces which have extension methods with same name, are included at different levels (most inner included namespace will have priority).

What is extension method with example?

Extension methods enable you to "add" methods to existing types without creating a new derived type, recompiling, or otherwise modifying the original type. Extension methods are static methods, but they're called as if they were instance methods on the extended type.

What is extension method in OOP?

In object-oriented computer programming, an extension method is a method added to an object after the original object was compiled. The modified object is often a class, a prototype or a type. Extension methods are features of some object-oriented programming languages.

What is extension method in C++?

An extension method allows you to add functionality to an existing type without modifying the original type or creating a derived type (and without needing to recompile the code containing the type that is extended.)


1 Answers

  1. Remove the ambiguity by redefining or eliminating one of the methods at the source. You don't need redundancy.
  2. If you do not control the source, include only one of them in your class file via the using directive.
  3. If you still need both namespaces in the given class file, invoke the version you wish simply as a static class call, unambiguously identifying the method via the (potentially fully qualified) class name.
 Abc.Xyz.ExtensionsClass.NoNull(database.ExecuteScalar(), string.Empty);  // <Abc.Xyz.> is only necessary if the classes themselves match names  // if not, only <ClassName>.<MethodName> is needed 
like image 122
Anthony Pegram Avatar answered Oct 24 '22 17:10

Anthony Pegram