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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T20:31:20+00:00 2026-05-24T20:31:20+00:00

I have the following method: public MethodInfo FancyGetMethodInfo (object obj, string methodName, Type[] methodSignature)

  • 0

I have the following method:

    public MethodInfo FancyGetMethodInfo
         (object obj, string methodName, Type[] methodSignature)
    {
        return obj.GetType().GetMethod(methodName, methodSignature);
    }

and for the sake of an example I have in my object I am passing the two methods

    public int Subtract(int a, int b) { return a - b; }

    public int Add(params int[] a) { return a.Sum(); }

And when I execute the following lines I get these results:

        var SubMethod = FancyGetMethodInfo(obj, "Subtract", 
                         new Type[] { typeof(int), typeof(int) });
        //I get a MethodInfo
        var AddMethod = FancyGetMethodInfo(obj, "Add", 
                         new Type[] { typeof(int), typeof(int) }); 
        //I get a Null reference

I’m sure this is because of the params in Add. Is there a clean way to get reference to my MethodInfo for my Add method given an arbitrary sized Type[] containing ints and only having access to the provided variables in my FancyGetMethodInfo method?

Edit:
More eloquently put by Jon Skeet, I want to perform the same binding as the C# compiler does in my method. it needs to work properly with subclasses, implicit casting, arbitrary length params, etc…

Also as requested, a relevant link here is: Determining if a parameter uses "params" using reflection in C#? which will get me as far as knowing that the method has a params.

  • 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-24T20:31:22+00:00Added an answer on May 24, 2026 at 8:31 pm

    Well, you could:

    • Find all the methods with the right name
    • For each method:
      • Check whether the last parameter in the method is a “params” array (as per this question; thanks yas4891)
      • Check whether the parameter types you’ve been given up to but not including the last parameter match the ones on the method
      • Check whether the remaining parameter types all match the array element type of the final parameter

    Note that once you’d found a match (and not found a match that didn’t require this params expansion) you’d then need to remember that so that you could deal with the arguments appropriately later (assuming you want to actually call the method).

    It’s a bit of a faff, to be honest… the way I see it you have three options:

    • Change the caller to pass in typeof(int[]) and your existing code will work
    • Change FancyGetMethodInfo to handle parameter arrays as described above
    • Change Sum to accept two integers (or possibly have various overloads)

    It’s up to you which route you choose…

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

Sidebar

Related Questions

I have the following method public static void SerializeToXMLFile(Object obj,Type type, string fileName) {
I have the following method: public object[] GetEventsByUser(DateTime start, DateTime end, string fullUrl) The
I have a following method: public IQueryable<Profile> FindAllProfiles(string CountryFrom, string CountryLoc) { return db.Profiles.Where(p
I have the following method: public IQueryable<Profile> FindAllProfiles(string CountryFrom, string CountryLoc) { return db.Profiles.Where(p
I have the following method signature: public void MyFunction(Object[,] obj) I create this object:
I have the following method: public IEnumerable<Foo> GetFoo(int x, string y) { return from
I have the following method: public void PutFile(string ID, Stream content) { try {
I have the following method : public List<string> someMethod() { // populate list of
I have the following method and interface: public object ProcessRules(List<IRule> rules) { foreach(IRule rule
I have the following method: public bool IsValid { get { return (GetRuleViolations().Count() ==

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.