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 3934928
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T23:46:19+00:00 2026-05-19T23:46:19+00:00

I was just going through some articles on reflection and generics, and I came

  • 0

I was just going through some articles on reflection and generics, and I came across the following code:

public static T CreateControlInstance<T>(FormControl parent) 
    where T : FormControl
{            
    T control = GetControlInstance<T>(parent);
    //do something with control
} 

public static T GetControlInstance<T>(FormControl parent)
{
      return (T)Activator.CreateInstance(typeof(T), new object[] { parent });
} 

These methods were used like this:

MyButton b = CreateControlInstance<MyButton>(SomeformInstance);

Many controls were created this way. I would just like to know:

Q1. What are the advantages of this approach?

Q2. What are the advantages of this approach, given that the object instance types are known at compile time? (I’m assuming that the button and FormControl were somehow related to System.Windows.Forms.Control)

Edit:
I found something similar being done here
Create instance of generic type?

Basically i want to create type(of known type) from strings which got read at runtime?
I wanted to avoid long list of if-then-else in creating objects of specific type depending on the string..but didn’t have a clue.
Any one has better solution so that reflection can be avoided to create elements of known type.
End Edit

  • 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-05-19T23:46:20+00:00Added an answer on May 19, 2026 at 11:46 pm

    The problem with generics is that you can’t define a constraint on a complex constructor. The only constraint is the availability of an empty constructor.

    public static T CreateInstance<T>() where T : new()
    {
      return new T();
    }
    

    However, when you want to pass parameter, you’ll have to use other methods, such as Activator.CreateInstance. You can also use lambda.

    public static T CreateInstance<T>(Func<FormControl, T> builder, FormControl parent)
    {
      return builder(parent);
    }
    

    But you’ll have to provide a specific lambda for constructing your object, for each different object.

    MyButton b = CreateInstance<MyButton>(parent => new MyButton(parent), SomeformInstance);
    

    By using reflection, you can make code simpler and automatically use a pre-defined constructor. But by using lambda, you can use class that don’t match a given convention and fill other constructor arguments with you own data.

    var b2 = CreateInstance<MyOtherButton>(
      parent => new MyOtherButton("name", 42, parent), SomeformInstance
    );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was going through some code examples and came across this application which which
So I'm teaching a friends son some python programming, just going through control of
When going through some sample code, notice that setting the main Window Title in
I was going through some code at work and I found this inline FLAGS
I am going through some code and changing the database part of it to
I just downloaded MVC and I am going through a tutorial. Everything goes fine
I was going through some notes and I realized something is amiss. When emulating
I was just going through the new MVC Music Store tutorial at asp.net. One
I am going to develop ICQ client and I just wonder where can I
I'm going to learn Qt and I just want to know what parts of

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.