Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to create an instance of an class without running ANY code from the class? (no ctor, no field initializations)

I have created an engine that takes in 3rd party plugins (DLL's) that implement an interface.

Since I have no control over the code that gets plugged in, I want to be able to run 1 specific method (from the interface) from the correct class (GetTypes loop untill I find the interfaced class ).

Since anyone can create nice constructor code that executes on Activator.CreateInstance, I can solve that by using FormatterServices.GetUninitializedObject. But that does not help when code is being initialized on fields in the class.

public class myclass : myinterface {

  public someotherclass name = new someotherclass()

  public myclass() {
     //Unknown code
  }

  //I only want this run.
  public string MyProperty{
    get {
      return "ANiceConstString";
    }
  }
}

The problem with both ways (CreateInstance/GetUninitializedObject) is that the constructor of someotherclass will be run.

Before you start analyze my needs. This is only run in the initializing of the engine to get a set of standard values. If this get'er relies on other initialized values the "plugin" will be marked as failed as there is no valid value returned. If not marked as failed, later on the class will be loaded properly with Activator.CreateInstance().

So stick to this question: Does .Net support any way to create an 100% non-initialized class?

Update for the answers. I tested this before I posted my question.

For the answer that someotherclass wont run, I allready tested that and it is run if static.

public class myclass : myinterface {

    static Tutle test;

    public myclass () {
         test = new Tutle();
    }

    public class Tutle {
        public Tutle() {
            MessageBox.Show("RUN!");
        }
    }
}

CreateInstance shows the messagebox. GetUninitializedObject does not.

public class myclass : myinterface {

    static Tutle test = new Tutle();

    public myclass () {
    }

    public class Tutle {
        public Tutle() {
            MessageBox.Show("RUN!");
        }
    }
}

CreateInstance shows the messagebox. GetUninitializedObject shows the messagebox.

Is there a way to get around static field intializers and ctors?

like image 664
Wolf5 Avatar asked Jan 03 '13 09:01

Wolf5


People also ask

What class can run without an instance of it being created?

Static Methods A static method of a class can be called without an instance of that class. As a result, static methods don't have any access to instance variables, or instance fields, because instance variables store information about an instance of a class.

What is called when you create an instance of a class?

Note: The phrase "instantiating a class" means the same thing as "creating an object." When you create an object, you are creating an "instance" of a class, therefore "instantiating" a class.

How do you initialize an instance of a class in Java?

To initialize a class member variable, put the initialization code in a static initialization block, as the following section shows. To initialize an instance member variable, put the initialization code in a constructor.

Can we create an instance of private class?

No, object of a class having private constructor cannot be instantiated from outside of the class.


1 Answers

Simply:

var obj = (myclass)FormatterServices.GetUninitializedObject(typeof(myclass));

That will not run the constructor / field initializers. At all. It will not run the constructor for someotherclass; name will be null.

It will, however, execute any static constructor that exists, if necessary under standard .NET rules.

HOWEVER! I should note that this method is not intended for ad-hoc usage; its primary intent is for use in serializers and remoting engines. There is a very good chance that the types will not work correctly if created in this way, if you have not subsequently taken steps to put them back into a valid state (which any serializer / remoting engine would be sure to do).

like image 114
Marc Gravell Avatar answered Oct 16 '22 11:10

Marc Gravell