第一个代码示例:如何从父母和孩子扩展方法具有相同的名称
public class Parent
{
}
public static class ParentExtension
{
public static void DoSomething<T>(this T element) where T : Parent
{
...
}
}
public class Child : Parent
{
}
public static class ChildExtension
{
public static void DoSomething<T>(this T element) where T : Child
{
...
}
}
//Trying to call child extension class
var child = new Child();
child.DoSomething(); //Actually calls the parent extension method even though it is a child class
那么,是不是可以做到什么,我在这里做什么? 我认为最具体的延伸将被拿起,但显然并非如此。
,为什么孩子一偶存在?? –
这可能会为您澄清:https://blogs.msdn.microsoft.com/ericlippert/2009/12/10/constraints-are-not-part-of-the-signature/ – NWard
[This](http:/ /stackoverflow.com/questions/31788804/how-to-hide-extension-methods-from-derived-classes?rq=1)似乎相关。 –