c# - Is it possible to override a non-virtual method? -
Is there any way to override a non-virtual method? Or something that gives similar results (except for making a new method to call the desired method)?
I want to override a method from Microsoft.Xna.Framework.Graphics.GraphicsDevice
in the brain with unit testing.
No, you can not override a non-virtual method. The closest thing is that you can hide the method by making a new
method with the same name, but it is not appropriate because it breaks good design principles.
But you will not have execution time while hiding a method. Polymorcic transmission method will call a true virtual method like this call:
System usage Tax; Square example {static zero} (Foo f = new Foo (); fm (); foo b = new bar (); bm ();}} square foo {public zero m () {Console.WriteLine ("Foo In this example, both the
Call M / s> Prints Printand the
Foo.M
As you can see from this perspective, you can get a new implementation for a method, as long as the object Reference is not of the correct derivative type, takeI suggest that you do not want to hide the bases in this manner.
I have those people I go to the side of the default behavior of C #, which by default is to go to non-virtual (as opposed) methods, I go ahead and say that the classes are also closed by default. Should be done. The legacy is difficult to design properly and the fact is that there is a method that is not marked for being virtual INTRODUCTION indicates that the intention of the method is never intended to override the writer of that method.
Edit: "Execution Time Polemorphic Dispatch" :
I mean this is the default behavior when you perform virtual time while executing time Happens at. For example, instead of defining my non-virtual method, for example, in my previous code example, I actually defined a virtual method and a right override method.
If I
bfu
in that case, CLR will correctly determine the type of object that is theb
reference to thebar
and calls toM
appropriate.
Comments
Post a Comment