Replies: 15 comments
-
How will this work across assemblies? |
Beta Was this translation helpful? Give feedback.
-
Target Type assembly <---ref--- Extension method Assembly <---- ref ---- Target Type inheritor with extension method override assembly. According to topic example: |
Beta Was this translation helpful? Give feedback.
-
This is likely a dupe of my old issue #310, and as far as I know, you cannot reliably declare virtual extension methods across multiple assemblies. The whole group of methods must be defined in a single assembly or you have to use dynamic features to implement them. |
Beta Was this translation helpful? Give feedback.
-
@orthoxerox I don't see "whole group of methods must be defined in a single assembly" limitation for this proposal. Please provide the case what exactly is impossible. Both #1061 #310 have the same goals but with different implementation details. |
Beta Was this translation helpful? Give feedback.
-
Okay, let me try to come up with an example:
public class A
{
}
public static class B
{
public virtual void PrintA(this A a)
{
Console.WriteLine("This is A");
}
}
public class C : A
{
}
public static class D
{
public override void PrintA(this C c)
{
Console.WriteLine("This is C");
}
}
A a = new C();
a.PrintA(); //what will happen here? |
Beta Was this translation helpful? Give feedback.
-
@orthoxerox, this proposal does not allows to override extension methods globally in a static class. public static class D
{
public override void PrintA(this C c)
{
Console.WriteLine("This is C");
}
} The only place to override is in the C assembly. public class C : A
{
override void B.PrintA()
{
Console.WriteLine("This is C !!!");
}
} So the extension method cannot override another extension method. |
Beta Was this translation helpful? Give feedback.
-
@dmitriyse Hm, if you are not allowed to declare overrides in an assembly other than the pseudo-receiver's one, then I guess it could work. C would implement the hidden interface from B etc. This doesn't solve the same use case as #310, though. And doesn't solve the use case of adding optimized LINQ methods to a third-party collection type. |
Beta Was this translation helpful? Give feedback.
-
Third-party collections is exactly best place to provide optimized LINQ method implementation. |
Beta Was this translation helpful? Give feedback.
-
What if that library is no longer supported or you cannot upgrade to the version in which the developer implemented optimized methods due to other unrelated breaking changes? |
Beta Was this translation helpful? Give feedback.
-
Probably you wants too much from the CLR/Compiler. It's the same that you want's hack up any third party library method. May be you need CLR level iterception. Or LINQ-like library should provide some hooks or extensibility points (the same approach as in ASP.Net Core.) |
Beta Was this translation helpful? Give feedback.
-
Oh, my poor intellisense. |
Beta Was this translation helpful? Give feedback.
-
@jnm2 |
Beta Was this translation helpful? Give feedback.
-
Also: Oh, my poor generated API contract file. |
Beta Was this translation helpful? Give feedback.
-
Hi All,
|
Beta Was this translation helpful? Give feedback.
-
Personally, I'm not really a fan of overriding extension methods that way. The way that I handle it is to essentially have a static event that my extension method calls and my event has a return value and a "handled" value that indicates whether the next item in the chain should be invoked. Here is a fairly large example showing an example (sorry about all the plumbling code). Also, this code was designed so that I could essentially do async event overriding.
|
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
This proposal adds to C# an ability to define virtual extension methods and override them.
Primary benefit of this proposal is ability to build advanced extensible Linq-like frameworks.
And even binary-compatible upgrade of existing Linq-like frameworks with extensibility features.
New syntax usage example:
C# 8.0+ will translate above example to this code (assuming that #52 already in the language):
(That's what CLR will see)
Beta Was this translation helpful? Give feedback.
All reactions