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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T00:29:31+00:00 2026-06-12T00:29:31+00:00

Is there a way to Define a default constructor for all classes in a

  • 0

Is there a way to Define a default constructor for all classes in a given assembly. For example I have classes like this in an assembly –

public class SomeClass
{
    public SomeClass(int x, int y)
    {
    }
}

These classes all require a default constructor but I don’t want to have to riddle all of these classes with a default constructor so how do I do this using reflection or something similar?? (Perhaps TypeBuilder.DefineDefaultConstructor?)

  • 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-12T00:29:33+00:00Added an answer on June 12, 2026 at 12:29 am

    These classes all require a default constructor but I don’t want to have to riddle all of these classes with a default constructor

    If the classes require a constructor, you should include it. Anything else will be a nightmare in terms of maintenance.

    The main issue with auto-generating a constructor is that the default constructor needs to know about the class in question, because it needs to properly initialize the values of the class. In your example, your default constructor would need to do something to set the internal state normally created with x and y. There is no way to know what that something should be without knowledge of the class, so the class is where it should be defined.

    That being said, there isn’t a direct way to just “add constructors to every type”. You could, potentially, use a tool like Cecil to rewrite the assembly with your modifications – basically, load the assembly, inspect each type, add the new information, and rewrite it. Alternatively, you could use some form of AOP tool, like PostSharp, to create constructors at compile time based on some attributes or similar.

    I would recommend against these approaches, however. I think that modifying the code itself and including the constructors when appropriate is the right approach.

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

Sidebar

Related Questions

Is there a way to have some kind of default constructor (like a C++
Given that one can define a class in VB Script, is there any way
Hi All is there any way to locally define a variable in a function
Say I have a class, something like the following; class MyClass { public: MyClass();
I have a class MyEntity, which does not have a default constructor (well, it
I have a base class and 4 derived classes. I store all my derived
Suppose I have a class with 3 constructors, a default (no argument) constructor, a
Trying to define an accessor method for default constructor parameter, i.e.: class Person (age:
Is there any way to view the default functions ( e.g., default copy constructor,
There's this dichotomy in the way we can create classes in f# which really

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.