0

Given

//all types of T inherit class name of BaseClass...
public void Test<T>(Action<T> CallBack){
  var obj = (T) Activator.CreateInstance<T>();
  //Debugger shows obj of proper type and shows its proper baseclass
  //now I want to change the base class and pass in a value to affect content
  var bc = new BaseClass("Parameter1");
  //downcast the original obj to type of baseclass
  var bcObj = (BaseClass)obj;    
  //and assign the downcast object the new BaseClass   
  bcObj = bc;
  //debugger shows bcObj IS the same as bc..  
  //but callback will not send the new bcObj content...     
  CallBack(obj);
}

Problem

When the CallBack happens the change of the base class is gone! What is seen is the same base class context seen right after Activator.Creatinstance.

Question

Is it not possible to downcast an object and assign values to it from another class instance of that type?

Possible Solution

I could contain the baseclass in the super class instead of inheriting.

JWP
  • 6,672
  • 3
  • 50
  • 74
  • You never set any values on obj or bcObj so why would you expect any change? – The Sharp Ninja Dec 10 '15 at 00:45
  • I don't get it. What's the point of all that `bc` code? You may as well delete everything after the `Activator.CreateInstance` line up to the line prior to `Callback(obj)` –  Dec 10 '15 at 00:45
  • Note the type T all inherit BaseClass, I'm trying to inject a new BaseClass into the parent class. – JWP Dec 10 '15 at 00:47
  • @TheSharpNinja I did change the entire bcObj to be the instance of bc. – JWP Dec 10 '15 at 00:50
  • Why do you expect that to change obj? These are references, not pointers you are dealing with. – The Sharp Ninja Dec 10 '15 at 00:51
  • @TheSharpNinja Fair enough, how do I get it to work? – JWP Dec 10 '15 at 00:55
  • You cannot. There isn't any pointer magic like you can do in C++. You don't have direct access to the instance of obj at the memory level, so you can't just declare a new instance of BaseClass and assign it to the same memory, which in C++ you could do. – The Sharp Ninja Dec 10 '15 at 01:00
  • 1
    If all type of `T` inherit from `BaseClass` you might want to give the `Test()` function a generic type constraint: `public void Test(Action CallBack) where T: BaseClass`... and probably, a constructor constraint too `public void Test(Action CallBack) where T: BaseClass, new()` – IronGeek Dec 10 '15 at 01:17
  • If you look at the code closely you'll see that I was trying to change the baseclass of object by injecting a new baseclass with parameter1. But, when the callback happened that new base class was gone! – JWP Jan 24 '16 at 17:17

1 Answers1

0

The idea; I thought, should have worked but did not. However, it's a bit of an anti-pattern indeed. I simply refactored the code to ensure strict separation of concerns and created a new class in the inheritance chain. That solved the problem. I simply had too much stuff going on in one of the base classes.

JWP
  • 6,672
  • 3
  • 50
  • 74