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

  • Home
  • SEARCH
  • 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 8068773
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T12:47:53+00:00 2026-06-05T12:47:53+00:00

Possible Duplicate: Invoking methods with optional parameters through reflection Name of the constructor arguments

  • 0

Possible Duplicate:
Invoking methods with optional parameters through reflection
Name of the constructor arguments in c#

Right now I am constructing objects using reflection. I am using this to fill out API documentation. In many cases I want a non-default constructor, but sometimes they have optional parameters. These optional parameters need to be overridden with a new object other than the default. The problem is I cant figure out how to get them. The normal parameters are easy with constructorInfo.GetParameters(), however it seems the optional do not come back. Am I missing something here?

Sample code:

            ConstructorInfo[] constructorInfoList = type.GetConstructors(BindingFlags.Instance | BindingFlags.Public);
            foreach (ConstructorInfo constructorInfo in constructorInfoList)
            {
                var parameters = constructorInfo.GetParameters();
                if (parameters.Count() > 0)
                {

Answer: It turns out they do come back… this was user error.

Sample:

void Main()
{
    var ctors = typeof(Foo).GetConstructors();
    foreach(var ctor in ctors)
    {
        foreach(var param in ctor.GetParameters())
        {
            Console.WriteLine("Name: {0} Optional: {1}", param.Name, param.IsOptional);
        }
    }   
}

public class Foo
{
    public Foo(string option1, string option2 = "")
    {
    }
}

Output:

Name: option1 Optional: False
Name: option2 Optional: True

  • 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-05T12:47:56+00:00Added an answer on June 5, 2026 at 12:47 pm

    Possible duplicate. It appears that you can call the parameter, but have to set the values manually.

    I found a similar problem here:

    Invoking methods with optional parameters through reflection

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

Sidebar

Related Questions

Possible Duplicate: !function(){ }() vs (function(){ })() So I was just reading through the
Possible Duplicate: PHP get all arguments as array? Within a javascript function arguments always
Possible Duplicate: thread with multiple parameters How does one thread a sub with two
Possible Duplicate: When does invoking a member function on a null instance result in
Possible Duplicate: When does invoking a member function on a null instance result in
Possible Duplicate: How can I pre-set arguments in JavaScript function call? (Partial Function Application)
Possible Duplicate: How to distinguish between left and right mouse click with jQuery? On
Possible Duplicate: How do I use define_method to create class methods? I am trying
Possible Duplicate: Best practices to test protected methods with PHPUnit class Footer { private
Possible Duplicate: How do I make a request using HTTP basic authentication with PHP

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.