Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9082041
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T20:27:44+00:00 2026-06-16T20:27:44+00:00

Possible Duplicate: Activator.CreateInstance – How to create instances of classes that have parameterized constructors

  • 0

Possible Duplicate:
Activator.CreateInstance – How to create instances of classes that have parameterized constructors

I was wondering how to create an object of a type determined at runtime without using the default constructor.

That is, I have BaseClass and various child classes.

Type type; //this variable will be one of the child classes
BaseClass base = Activator.CreateInstance(type);

This allows me to create a child class object with the default constructor, but I’d like to call a specific constructor. I know all of the child classes have a constructor taking certain parameters, so I have no worries of that constructor not existing. I found this question, but the best I can get there is a single string parameter. Is this doable?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T20:27:45+00:00Added an answer on June 16, 2026 at 8:27 pm

    Three options:

    • Use Type.GetConstructor and then ConstructorInfo.Invoke
    • Use one of the overloads of Activator.CreateInstance which takes arguments.
    • If you already know all the possible types beforehand, create a Dictionary<Type, Func<string, string, YourBaseType>> or whatever’s appropriate

    The third option requires you to change your factory code each time you add a new type, of course – but it’s only a single line.

    Personally I like the first option, as it gives you the most control (rather than relying on Activator.CreateInstance finding the best matching constructor at execution time) – and if this is performance-sensitive code, you could build a dictionary of delegates at execution time by discovering the constructors, then using expression trees. (As far as I can tell, you can’t build a delegate from a constructor using Delegate.CreateDelegate, which is somewhat annoying.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Create an empty object in JavaScript with {} or new Object()? When
Possible Duplicate: Restore WindowState from Minimized I have window which is normally hidden in
Possible Duplicate: Does every Core Data Relationship have to have an Inverse? I have
Possible Duplicate: Can you write object oriented code in C? Hi, can someone point
Possible Duplicate: What is 0x10 in decimal? I notice that Console.WriteLine(18); writes 18, but
Possible Duplicate: When must we use extern alias keyword in C#? I have an
Possible Duplicate: RegEx match open tags except XHTML self-contained tags I have a HTML
Possible Duplicate: When do you use the “this” keyword? Hello, I understand that the
Possible Duplicate: Floating point division vs floating point multiplication recently, i have written a
Possible Duplicate: Understanding NSRunLoop Till now I know that every thread has its own

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.