简体   繁体   English

如何通过静态类或其他方式更改密封类方法?

[英]how to change sealed class method by static class or another way?

I want to override a method of the Sealed Class from a static class . 我想从静态类中重写Sealed类的方法。

For example: 例如:

public class MyClass
{
    public virtual void MyMethod()
    {
        Console.WriteLine("I'm MyMethod from MyClass");
    }
}

public sealed class MySealedClass : MyClass
{
    public override void MyMethod()
    {
        Console.WriteLine("I'm MyMethod from MySealedClass");
    }
}

The MyClass have a virtual method by name MyMethod . MyClass具有一个名为MyMethod的虚拟方法。

In the sealed class this method has been override for self and i want to be written that again for another job by this static class: 在密封类中,此方法已被self覆盖,我想通过该静态类再次将其写入另一项工作:

public static class ClassManager
{
    public static void MyMethod(this MySealedClass msc)
    {
        Console.WriteLine("I'm MyMethod from ClassManager");
    }
}

Now, we call that static method's from Program class to run it: 现在,我们从Program类调用该静态方法来运行它:

class Program
{
    static void Main(string[] args)
    {
        new MySealedClass().MyMethod();
    }
}

But this result called MySealedClass methods not called my static class method's !! 但是这个结果称为MySealedClass方法,而不是静态类方法!

I'm MyMethod from MySealedClass 我是MySealedClass的MyMethod

Please help me, how to change sealed class method by static class or another way ? 请帮助我,如何通过静态类或其他方式更改密封类方法?

You can't do that.Extensions methods are considered only if there is no method with that signature in your type. 您不能那样做。只有在您的类型中没有带有该签名的方法时,才考虑使用扩展方法。

You need to call your static method explicitly: 您需要显式调用static方法:

ClassManager.MyMethod(new MySealedClass());

It isn't possible. 这是不可能的。 From MSDN : MSDN

An extension method with the same name and signature as an interface or class method will never be called . 与接口或类方法具有相同名称和签名的扩展方法将永远不会被调用 At compile time, extension methods always have lower priority than instance methods defined in the type itself. 在编译时, 扩展方法的优先级始终比类型本身定义的实例方法低 In other words, if a type has a method named Process(int i), and you have an extension method with the same signature, the compiler will always bind to the instance method. 换句话说,如果类型具有名为Process(int i)的方法,并且您具有具有相同签名的扩展方法,则编译器将始终绑定到实例方法。

From the C# spec: 根据C#规范:

The preceding rules (the set of rules specified in the spec) mean that instance methods take precedence over extension methods , that extension methods available in inner namespace declarations take precedence over extension methods available in outer namespace declarations, and that extension methods declared directly in a namespace take precedence over extension methods imported into that same namespace with a using namespace directive. 前面的规则(规范中指定的规则集)意味着实例方法优先于扩展方法 ,内部命名空间声明中可用的扩展方法优先于外部命名空间声明中可用的扩展方法,并且扩展方法直接在名称空间优先于使用using namespace指令导入到同一名称空间的扩展方法。

You'll have to invoke the method via your static class, and not as an extension method. 您将必须通过您的静态类调用该方法,而不是作为扩展方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM