views:

983

answers:

9

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.

+3  A: 

At least 1 ctor has to be called. The only way around it i see is containment. have the class inside or referncing the other class.

mattlant
This is the path that is getting me the most results. Thanks!
casademora
+2  A: 

Can you make your base constructors private?

Swati
@Swati: or he could add a private parameterless constructor to his proxy class which calls the correct base class constructor.
sixlettervariables
I need to have the proxy become subclassed itself via Rhino mocks (hence the proxy). Normally, i would start moving code around, but no tests means bad things if I do that :(
casademora
+6  A: 

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.

Actually, if you read my comment below, there is a way, although I wouldn't recommend it in most cases.
Neil Whitaker
A: 

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

Jakub Šturc
+1  A: 

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

AlbertEin
+2  A: 

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
   }
}
Jake Pearson
+1  A: 

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.

Lucas
A: 

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.

casademora
The implicit default constructor still calling the parameter-less constructor of BaseClass.
recursive
+4  A: 

There is a way to create an object without calling any 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.

Neil Whitaker
That's bonkers. My whole world's upside down!
recursive