Search code examples
c#microsoft-commerce-server

Extension methods for multiple types


I have an object of type Product and type Variant. Variant and Product have the same structure, but are two different types, and so I can't make just one method to encompass both. Is it possible to make an extension method that would accept both of these types?


Solution

  • You cannot do that unless Product and Variant have common base class or interface.

    If they have common base class or interface then you can try to write extension method for it e.g.

    public static void MyMethod(this ICommonInterface obj) {}
    

    Otherwise you'll have to create two separate extensions methods. You can try to extract common code to a separate method which will be called from your extension methods.