c# - Generic extension method resolution fails -
The following program does not compile, because in line with the error, the compiler uses a I have read two blog posts about Generic Method Resolution, which is second from John Skate and another from Eric Lipper, but I could not find an explanation or a way to solve my problem. Obviously, two methods will work with different names, but I like the fact that you have only one way for those cases. The method determines the resolution. See blog posts for exact rules. What does it mean? The compiler will see whether it can achieve an exact match, if for some reason it can not be found, then it will get the next possible compatible method further. Let's first compile this code by Because Code> closer to that meaning Which can take any form in the form of a parameter, and no conversion is needed for it. Why the This is because you have the time type of compilation of the variable Go back to your question. Why Again, as the first You can change this variable type variable to T parameter As a resolution, which is the
list & lt; T & gt; does not fit in the usual obstacles of single
T , fails. The compiler does not believe that there is another method that can be used if I remove the single-
T method, then the compiler will search the method correctly for many objects.
Namespace testing {Using the system. Colllections.Generic; Public Interface SomeInterface {} Public Segment Some Impressions: Some Interface {} Public Stable Class Extension Techniques {// To exclude this line, the compiler chose the correct method on the line that puts an error under public static zero method Method & lt; T & gt; (This T parameter) where T: some interface {} public static zero method & lt; T & gt; (This is IEnumerable & lt; T & gt; Parameters) where T: some interface {}} class program {static zero main () {var example = new implementation of something new; Var instance = new list & lt; Some Implementation & gt; (); // work example. Method (); // Error 1 type 'System.Collections.Generic.List & lt; Test.SomeImplementation & gt; // General Type or Method // 'Test.ExtensionMethods.Method & lt; T & gt; (T) 'type parameter can not be used as' T'. 'System.clalls. GenericList & lt; Test.Some Implementation & gt; There is no underlying reference conversion from. 'Test. Instances.Method (for some interfaces); // Tasks (e.g. IEnumerable & lt; Some Implementation & gt;) Method (); }}}}
SomeInterface public static class extension rule (public static zero method
method (t)
method (t)
method (IEnumerable )
list & lt; T & gt; , so it's
There is a need for reference conversion from
IEnumerable & lt; T & gt; from the list & lt; T & gt; which is close but is not making any conversions at all.
instances.Method (); does not compile?
method (ienumerable & lt; T & gt;) we need some context conversion, so obviously it is not close Now we are left with only one method which is very close to
method & lt; T & gt; But the problem is that you've compromised it with
SomeInterface and explicitly
list & lt; Some impressions & gt; ()
SomeInterface is not convertible. The problem (usually guessing) is checking the general obstacles, after which the compiler chooses the maximum surcharge. This invalidates the best surcharge chosen in this case.
IEnumerable & lt; SomeImplementation & gt; which will work and now you know.
IEnumerable & lt; Some Implementation & gt; Example = New List & lt; Some proportionate & gt; ();
Comments
Post a Comment