English 中文(简体)
Invoking an instance method without invoking constructor
原标题:

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?

最佳回答

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:

  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.

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





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签