I want to override a method of the Sealed Class from a static class.
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.
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:
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:
class Program
{
static void Main(string[] args)
{
new MySealedClass().MyMethod();
}
}
But this result called MySealedClass methods not called my static class method's !!
I'm MyMethod from MySealedClass
Please help me, how to change sealed class method by static class or another way ?
It isn't possible. From MSDN:
From the C# spec:
You'll have to invoke the method via your static class, and not as an extension method.