Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to explicitly indicate which ExtensionClass to use

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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?

>Solution :

(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?

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading