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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T02:52:43+00:00 2026-05-14T02:52:43+00:00

Reading existing code at work, I wondered how come this could work. I have

  • 0

Reading existing code at work, I wondered how come this could work. I have a class defined in an assembly :

[Serializable]
public class A
{
    private readonly string _name;
    private A(string name)
    {
        _name = name;
    }
}

And in another assembly :

public void f(Type t) {
    object o = Activator.CreateInstance(t);
}

and that simple call f(typeof(A))

I expected an exception about the lack of a parameterless constructor because AFAIK, if a ctor is declared, the compiler isn’t supposed to generate the default public parameterless constructor.

This code runs under .NET 2.0.

[EDIT] I’m sorry but I misread the actual code… The sample I provided doesn’t illustrate it. I accepted JonH answer because it provided a good piece of information.

  • 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-14T02:52:43+00:00Added an answer on May 14, 2026 at 2:52 am

    See this: Creating instance of type without default constructor in C# using reflection

    Here’s to the future also, this is in regards to C# 4.0:

    Posted by Microsoft on 1/4/2010 at
    2:08 PM
    We have reviewed your bug and
    have determined that the behavior that
    you described is by design. We are now
    archiving this issue. Thanks for using
    Visual Studio and the .Net Framework!

    There are many overloads of Activator.CreateInstance, the one that
    takes a single type parameter only
    invokes the default parameterless
    constructor. Constructors that take an
    optional parameter like the one in
    your example are not default
    constructors. To invoke them you need
    to:

    1. use an overload that takes an argument array
    2. Pass in Type.Missing as the argument
    3. Specify OptionalParamBinding in the BindingFlags

    Here is an example:

    Activator.CreateInstance(typeof(MyClassName),
     BindingFlags.CreateInstance |
     BindingFlags.Public |
     BindingFlags.Instance |
     BindingFlags.OptionalParamBinding,
     null, new Object[] {Type.Missing}, null);
    

    Thanks,

    Weitao Su

    Microsoft Corp.

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

Sidebar

Related Questions

Reading this question I found this as (note the quotation marks) code to solve
I am currently reading an existing code in MS Visual C++ 6.0. I notice
I have been trying to learn how to add testing to existing code --
I am reading existing code. I noticed that there are many data object files
I have a very simple managed bean LOB which is used for reading existing
Having read an existing post on stackoverflow and done some reading around on the
Reading over the responses to this question Disadvantages of Test Driven Development? I got
Reading through this question on multi-threaded javascript, I was wondering if there would be
Reading source code of sample projects, such as Beast and Bort, are recommended as
The following code doesn't work as I intuitively expect it to: function MyObject(input) {

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.