Invoking an instance method without invoking constructor
Let's say I h开发者_JAVA技巧ave the following class which I am not allowed to change:
public class C
{
public C() { CreateSideEffects(); }
public void M() { DoSomethingUseful(); }
}
and I have to call M without calling the constructor. Is it possible?
Even if it's NOT A GOOD IDEA, yes we can ;) with FormatterServices.GetUninitializedObject .
C uninitializedC = (C)FormatterServices.GetUninitializedObject(typeof(C));
uninitializedC.M();
No. Because C.M()
is an instance method, you need to create an instance, which means calling the constructor.
Is C
a class that your team owns? If it is, but you are under orders to leave it alone, you'd do well to lobby for either:
- Those side-effects to be refactored and removed
- The
C.M()
method functionality moved out to another class or made static.
If C
is from a 3rd-party, you're going to have trouble, and may have to replicate the functionality of C.M()
in a method you do own.
Yes, you can!
Needless to say that this is a bad design, but you already know that and cannot change it. If you must, you can attempt at partial mocking the class.
EDIT: Just realized that my example uses Java not C#. However, @Guillaume offers the code sample for C#. Apparently, it is even built into the runtime API!
In Java, With Mockito, this does work:
C c = Mockito.mock(C);
Mockito.doCallRealMethod().when(c).M();
// If M() isn't a void method
// when(c.M()).thenCallRealMethod();
c.M();
However, in this case M()
cannot depend on any state set in the constructor.
For more info on partial mocking, check out this FAQ Question. However, mocking is mainly used for testing.
In order to invoke an instance method, you need an instance! And - for good reasons - the only way to obtain one is via the constructor. Otherwise the whole object may be in an indeterminate or useless state because initializations haven't been made. So even if there was some kind of hack, it would be no good choice at all!
The only kind of class member you can invoke without an instance are static methods.
I won't tell you this is a bad idea since it sounds like you've been told that enough. Oh, sorry I guess I just did... anyway here is how to do it:
using System.Runtime.Serialization;
C myInstance = FormatterServices.GetUninitializedObject(typeof(C));
myInstance.M();
The 'GetUninitializedObject' method above returns an instance of an object without calling any instance ctor (obviously any static type ctor will still run). Then, you can poke instance fields if needed or simply call methods.
Again, a bad idea as a whole ;)
精彩评论