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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T02:32:50+00:00 2026-05-24T02:32:50+00:00

I am trying to work with the class found in this post but it

  • 0

I am trying to work with the class found in this post but it needs a MethodBase to run.

I read What is the fastest way to get a MethodBase object? but i couldn’t get any solution to work.

What i need to do is to get the MethodBase object from a function.

For example getting the MethodBase for the static function WriteLine() of the class Console or getting the MethodBase for the non-static function Add() of a List<>.

Thanks for your help!

  • 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-24T02:32:50+00:00Added an answer on May 24, 2026 at 2:32 am

    Method 1

    You can use reflection directly:

    MethodBase writeLine = typeof(Console).GetMethod(
        "WriteLine", // Name of the method
        BindingFlags.Static | BindingFlags.Public, // We want a public static method
        null,
        new[] { typeof(string), typeof(object[]) }, // WriteLine(string, object[]),
        null
    );
    

    In the case of Console.Writeline(), there are many overloads for that method. You will need to use the additional parameters of GetMethod to retrieve the correct one.

    If the method is generic and you do not know the type argument statically, you need to retrieve the MethodInfo for the open method and then parametrize it:

    // No need for the other parameters of GetMethod because there
    // is only one Add method on IList<T>
    MethodBase listAddGeneric = typeof(IList<>).GetMethod("Add");
    
    // listAddGeneric cannot be invoked because we did not specify T
    // Let's do that now:
    MethodBase listAddInt = listAddGeneric.MakeGenericMethod(typeof(int));
    // Now we have a reference to IList<int>.Add
    

    Method 2

    Some third-party libraries can help you with this. Using SixPack.Reflection, you can do the following:

    MethodBase writeLine = MethodReference.Get(
        // Actual argument values of WriteLine are ignored.
        // They are needed only to resolve the overload
        () => Console.WriteLine("", null)
    );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple Class Hierarchy that I am trying to get to work
I'm having a bit of trouble trying to get class variables to work in
So I have a factory class and I'm trying to work out what the
Programming Student here...trying to work on a project but I'm stuck. The project is
I have been trying to work my way through Project Euler, and have noticed
1st Question: I'm trying to make the create method work, but it appears that
I am trying to stop some events but stopPropagation does not work with live
I'm trying to get the permit method to work using the rails-authorization-plugin and authlogic
Hello i been trying to get a tokenizer to work using the boost library
I know it sounds stupid but: I've found this application written on Mono and

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.