views:

317

answers:

5

Let's say I have 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?

+7  A: 

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:

  1. Those side-effects to be refactored and removed
  2. 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.

Neil Barnwell
+1 for moving "side-effects". a constructor should **never** do any work except member assignment. heavy lifting should always be delegated to a post-ctor method to avoid exceptions on instantiations.
johnny g
@johnnyg Well, not necessarily always. If your objects are loaded via IoC, then you won't be able to initialise them because the interface might not have an `Initialise()` method, and you can't guarantee it would be called even if it did. If classes do lazy-initialisation when methods etc are called, that helps, but really it's not all that bad to do some work in the ctor. The issue is if the things it does cause actual proper side-effects and therefore cause trouble elsewhere.
Neil Barnwell
+1  A: 

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.

Dario
+4  A: 

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.

notnoop
Whoa, that is crossing the streams quite a bit, or else you'd get +1 just for being such an evil genius. :)
Neil Barnwell
@Neil, the question is whether it is possible or not. Contrary to what is being said, it IS possible. It's a different question on whether it is good or not.
notnoop
Yes I know, but it's a fine line on whether something is possible, versus whether it should be done. If someone asked if it were possible to survive a fall from a 2nd-floor window, you could say yes, but you'd still recommend highly that they don't do it. You might consider a white-lie and tell them no. :) I had no idea this was possible, and said as much in my answer. To all intents and purposes, it **is** impossible.
Neil Barnwell
Well, it depends on the context. e.g. If he's a tester, and he doesn't want to incur the cost of the database connection happening in the constructor. It's common to create a mock there for the sake of testing other non-database related methods. It's not uncommon, especially that testers cannot change the code, especially if it is legacy code.
notnoop
+8  A: 

Even if it's NOT A GOOD IDEA, yes we can ;) with FormatterServices.GetUninitializedObject .

C uninitializedC = (C)FormatterServices.GetUninitializedObject(typeof(C));
uninitializedC.M();
Guillaume
+1  A: 

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 ;)

csharptest.net