Question

I've got a (poorly written) base class that I want to wrap in a proxy object. The base class resembles the following:

public class BaseClass : SomeOtherBase 
{
   public BaseClass() {}

   public BaseClass(int someValue) {}

   //...more code, not important here
}

and, my proxy resembles:

public BaseClassProxy : BaseClass
{
  public BaseClassProxy(bool fakeOut){}
}

Without the "fakeOut" constructor, the base constructor is expected to be called. However, with it, I expected it to not be called. Either way, I either need a way to not call any base class constructors, or some other way to effectively proxy this (evil) class.

Was it helpful?

Solution

If you do not explicitly call any constructor in the base class, the parameterless constructor will be called implicitly. There's no way around it, you cannot instantiate a class without a constructor being called.

OTHER TIPS

There is a way to create an object without calling any instance constructors.

Before you proceed, be very sure you want to do it this way. 99% of the time this is the wrong solution.

This is how you do it:

FormatterServices.GetUninitializedObject(typeof(MyClass));

Call it in place of the object's constructor. It will create and return you an instance without calling any constructors or field initializers.

When you deserialize an object in WCF, it uses this method to create the object. When this happens, constructors and even field initializers are not run.

At least 1 ctor has to be called. The only way around it I see is containment. Have the class inside or referencing the other class.

If what you want is to not call either of the two base class constructors, this cannot be done.

C# class constructors must call base class constructors. If you don't call one explicitly, base( ) is implied. In your example, if you do not specify which base class constructor to call, it is the same as:

public BaseClassProxy : BaseClass
{
    public BaseClassProxy() : base() { }
}

If you prefer to use the other base class constructor, you can use:

public BaseClassProxy : BaseClass
{
    public BaseClassProxy() : base(someIntValue) { }
}

Either way, one of the two will be called, explicitly or implicitly.

I don't believe you can get around calling the constructor. But you could do something like this:

public class BaseClass : SomeOtherBase 
{
   public BaseClass() {}

   protected virtual void Setup()
   {
   }
}

public BaseClassProxy : BaseClass
{
   bool _fakeOut;
   protected BaseClassProxy(bool fakeOut)
   {
        _fakeOut = fakeOut;
        Setup();
   }

   public override void Setup()
   {
       if(_fakeOut)
       {
            base.Setup();
       }
       //Your other constructor code
   }
} 

When you create a BaseClassProxy object it NEEDS to create a instance of it's base class, so you need to call the base class constructor, what you can doo is choose wich one to call, like:

public BaseClassProxy (bool fakeOut) : base (10) {}

To call the second constructor instead of the first one

I am affraid that not base calling constructor isn't option.

I ended up doing something like this:

public class BaseClassProxy : BaseClass 
{
   public BaseClass BaseClass { get; private set; }

   public virtual int MethodINeedToOverride(){}
   public virtual string PropertyINeedToOverride() { get; protected set; }
}

This got me around some of the bad practices of the base class.

constructors are public by nature. do not use a constructor and use another for construction and make it private.so you would create an instance with no paramtersand call that function for constructing your object instance.

All right, here is an ugly solution to the problem of one class inheriting the constructors of another class that I didn't want to allow some of them to work. I was hoping to avoid using this in my class but here it is:

Here is my class constructor:

public MyClass();
{
   throw new Exception("Error: Must call constructor with parameters.");
}

OK now you were warned that it was ugly. No complaints please!

I wanted to force at least the minimal parameters from my main constructor without it allowing the inherited base constructor with no parameters.

I also believe that if you create a constructor and do not put the : base() after it that it will not call the base class constructor. And if you create constructors for all of the ones in the base class and provide the same exact parameters for them in the main class, that it will not pass through. But this can be tedious if you have a lot of constructors in the base class!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top