c# - Extension Method Collision -


this question has answer here:

i have 2 extension methods interface, 1 performs action on instance of class implementing interface , 1 on collection of classes implementing interface. problem compiler uses first extension method , creates compile error, cannot use collection version.

public interface isomeinterface { }  public class someclass : isomeinterface { }  public static class someclassextensions {     public static t dosomething<t>(this t item) t : class, isomeinterface     {         return item;     }      public static ienumerable<t> dosomething<t>(this ienumerable<t> items) t : class, isomeinterface     {         return items;     } }  internal class program {     private static void main()     {         var items = new[]         {             new someclass()         };          items.dosomething(); // compiler error     } } 

the error clear compiler trying use first extension method:

the type 'consoleapplication1.someclass[]' cannot used type parameter 't' in generic type or method 'someclassextensions.dosomething(t)'. there no implicit reference conversion 'consoleapplication1.someclass[]' 'consoleapplication1.isomeinterface'.

clearly can rename 1 of 2 extension methods that's last resort open source project , i'm trying avoid deviating established set of naming conventions.

i'm out of ideas on how force need work, perhaps there's way add or adjust generic type constraints?

this should

var items = new[] {     new someclass() };  items.dosomething<someclass>(); 

that's of course assuming items someclass[] .


Comments

Popular posts from this blog

Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12:test (default-test) on project.Error occurred in starting fork -

windows - Debug iNetMgr.exe unhandle exception System.Management.Automation.CmdletInvocationException -

configurationsection - activeMq-5.13.3 setup configurations for wildfly 10.0.0 -