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

16
  • 2
    Your question isn't very clear. Commented May 2, 2013 at 14:16
  • can you describe why can't you simple do public ExampleClass(string s) : base(s){} ? Commented May 2, 2013 at 14:19
  • public ExampleClass() : base("someValue") {} ----? Commented May 2, 2013 at 14:19
  • 1
    public ExampleClass() : base(String.Empty) {} Commented May 2, 2013 at 14:21
  • 1
    @Tuk This all makes no sense and immediately sounds like 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. Commented May 2, 2013 at 14:34

5 Answers 5

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

Sign up to request clarification or add additional context in comments.

1 Comment

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.
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
}

1 Comment

Hi James, thanks 4 your answer, please see my answers up there
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)
{
}

1 Comment

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
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).

Comments

-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);

1 Comment

op said, he can't modify ExampleClass or BaseClass

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.