3

For reasons out of the scope of the question, I'm needing to dynamically create an instance of a child class that inherits from a base class, calling a constructor that doesn't exist with an argument passed to the base class constructor

Using example below: should create an instance of ExampleClass sending value to argument1 of BaseClass.

class BaseClass
{
    public BaseClass()
    {

    }
    public BaseClass(string argument1)
    {
        //...
    }
}

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

EDIT: I made another topic where I explain the source or my problem: Entity Framework DbContext dynamic instatiation with custom Connection String

Community
  • 1
  • 1
Tuk
  • 143
  • 2
  • 10
  • 2
    Your question isn't very clear. – tnw May 02 '13 at 14:16
  • can you describe why can't you simple do `public ExampleClass(string s) : base(s){}` ? – Ilya Ivanov May 02 '13 at 14:19
  • public ExampleClass() : base("someValue") {} ----? – Jurica Smircic May 02 '13 at 14:19
  • Let me rephrase it: Need to dinamically "simulate" a constructor in ExampleClass that overrides the constructor in BaseClass that receives "argument1" – Tuk May 02 '13 at 14:20
  • 1
    `public ExampleClass() : base(String.Empty) {}` – valverij May 02 '13 at 14:21
  • Ok, I can't do that because ExampleClass is an auto-generated class and can't modify it permanently. – Tuk May 02 '13 at 14:21
  • So you want to dynamicly create a instance of `ExampleClass` using the Ctor of the `BaseClass` that takes `argument1`? I guess you can´t just define the missing Ctor in `ExampleClass`? – Mecaveli May 02 '13 at 14:21
  • Thanks Mecaveli, that's right, in the context of my application I just can't create that missing constructor, so I think there should be a work-around using reflection – Tuk May 02 '13 at 14:23
  • Is `ExampleClass` declared `partial` by chance since it´s auto generated? – Mecaveli May 02 '13 at 14:26
  • Yes it is Macaveli. thought about that, but but I'm intending to do this without creating a partial specification with that constructor. The explanation is that I'm developing a black-box framework and intent minimal configuration for users. – Tuk May 02 '13 at 14:29
  • You can't just pretend a constructor exists in your subclass that calls a particular constructor in your base class exists when it doesn't. What you want to do you cannot do. So you should reconsider your problem. – Kirk Woll May 02 '13 at 14:31
  • Thanks Kirk. I'm aware that using reflection it's even possible to create a Class on-the-fly, so I think there could be a way, but maybe not, I don't know... – Tuk May 02 '13 at 14:34
  • 1
    @Tuk This all makes no sense and immediately sounds like the [XY Problem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) in that you are asking us one thing but not stating what it is for and why it needs to be done this way. I can tell you now, it is **extremely likely** that there is a better way. There is no way to call that constructor unless the child calls it for you if you want a child reference. – Adam Houldsworth May 02 '13 at 14:34
  • Hi Adam, I see what you mean. So I should try sharing a broader scope of my problem (X). – Tuk May 02 '13 at 14:37
  • If it´s partial then declaring the overloaded Ctor in a partial implementation is by far the cleanest solution. Everything else is exiting but very hacky :) – Mecaveli May 02 '13 at 14:39
  • Hi Adam, there I posted a clear version of my problem, thanks for your advice. – Tuk May 02 '13 at 15:00

5 Answers5

2

If I understand it correct you can't modify ExampleClass but need to create an instance of it that uses a different constructor for the base class?

I belive there is not build in way in the framework to achive it, even with reflection. So your goal should be to bypass the framework and use MSIL

However, this topic I found on SO looks promissing.

Dynamically create type and call constructor of base-class

Community
  • 1
  • 1
Jürgen Steinblock
  • 30,746
  • 24
  • 119
  • 189
  • Hi! I see there could be a way. The linked issue makes me think I can create dinamically the "partial" type I cant create on code and include the constructor I need. – Tuk May 02 '13 at 15:01
0

Do you want to call the base class' constructor?

public ChildClass(string arg1) : base(arg1)
{
    //above will invoke base constructor that takes a string argument
}
James Wright
  • 3,000
  • 1
  • 18
  • 27
0

The only way I can see to do this is to add a ParentClass constructor. I doesn't have to do anything, so I don't see why you can't add it:

public ParentClass(string argument1) : BaseClass(argument1)
{
}
Mike
  • 141
  • 3
  • Thanks 4 your answer Mike, the problem is that in the context of my application I just can't create that missing constructor, so I think there should be a work-around using reflection – Tuk May 02 '13 at 14:24
0

Try this:

class ExampleClass : BaseClass
{
    public ExampleClass()
        : base(Properties.Settings.Default.MyArgument)
    {
    }
}

You can use Properties.Settings.Default.MyArgument in order to pass in the value of the argument you want use in the base class constructor without modifying the source code everytime the argument's value needs to be changed (just add it into the projects's Settings, application scoped).

Alex Filipovici
  • 31,789
  • 6
  • 54
  • 78
-1

If you can't add a non-default constructor to your ChildClass, then no you can't create an instance of ChildClass calling a base class constructors. Constructors are not inherited. What you need to then is to refactor your base class so that there is way to set the argument not using a constructor. For example see below:

class BaseClass
{
    public BaseClass()
    {

    }
    public BaseClass(string argument1)
    {
         Init(argument1);
    }

    public void Init(string argument1)
    {
        //...
    }
}

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

Then you can create an instance of your ExampleClass and call the Init method to initialize it:

 ExampleClass e = (ExampleClass)Activator.CreateInstance.... // or whatever
 e.Init(arugment);
shf301
  • 31,086
  • 2
  • 52
  • 86