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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T06:34:13+00:00 2026-05-26T06:34:13+00:00

I have a class with a private static method with an optional parameter. How

  • 0

I have a class with a private static method with an optional parameter. How do I invoke it from another class via Reflection? There is a similar question, but it does not address static method or optional parameters.

public class Foo {
    private static void Bar(string key = "") {
       // do stuff
    }
}

How do I invoke Foo.Bar("test") and Foo.Bar() (e.g. without passing the optional parameter)?

  • 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-26T06:34:14+00:00Added an answer on May 26, 2026 at 6:34 am

    Optional parameter values in C# are compiled by injection those values at the callsite. I.e. even though your code is

    Foo.Bar()
    

    The compiler actually generates a call like

    Foo.Bar("")
    

    When finding the method you need to treat the optional parameters as regular parameters.

    var method = typeof(Foo).GetMethod("Bar", BindingFlags.Static | BindingFlags.NonPublic);
    

    If you know exactly what values you want to invoke the method with you can do:

    method.Invoke(obj: null, parameters: new object[] { "Test" });
    

    If you only have some of the parameters and want to honor the values of the default ones you have to inspect the method’s ParameterInfo objects to see if the parameters are optional and what those values are. For example to print out the default values of those parameters you could use the following code:

    foreach (ParameterInfo pi in method.GetParameters())
    {
        if (pi.IsOptional)
        {
            Console.WriteLine(pi.Name + ": " + pi.DefaultValue);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose you have a private static method called Inst() which allows the class to
I have this class: public static class CsvWriter { private static StreamWriter _writer =
I have a class class foo { public: foo(); foo( int ); private: static
I have a base class that has a private static member: class Base {
Let's say I have a class called AppConfig: public static class AppConfig { private
So I have the following: public class Singleton { private Singleton(){} public static readonly
I have the following code: public partial class queryTerm : System.Web.UI.UserControl { private static
I have the following code: public class Test extends JFrame implements ActionListener{ private static
Let's say I have some Java code: public class SomeClass { static { private
I have a little problem. I have class private static class EfficientAdapter extends BaseAdapter

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.