Home > Enterprise >  How to explicitly indicate which ExtensionClass to use
How to explicitly indicate which ExtensionClass to use

Time:06-26

I have a class which has 2 ExtentionsClasses for it.

Both of them have methods with the same name but with different return types.

I have a service which contains some methods, I want to use method from 1 ExtentionClass in one method in this service, and method from second ExtentionClass in another method. When I am trying to do this, SomeMethod() from the method B() calls first ExtentensionClass, but not second.

class SomeService 
{
   public void A() 
   {
      var x = context.SomeMethod(); // SomeMethod - method from first ExtensionClass
   }

   public void B() 
   {
      var x = context.SomeMethod(); // SomeMethod - method from first ExtensionClass, 
                                    // but should be from second
   }
}

How can I explicitly indicate which ExtensionClass to use?

CodePudding user response:

(I assume that Somethod() are extension methods for the type of context).

What you can always do, is to call the extension method like a normal static method, e.g:

var x = ExtensionClass1.SomeMethod(context);
var y = ExtensionClass2.SomeMethod(context);

I don't know of any other way.

I think it's a bad design to have two methods with the same name, but different functionality / return type. So maybe just rename one of the extension methods?

  • Related