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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T07:50:55+00:00 2026-05-13T07:50:55+00:00

How can I find a generic overloaded method? For example, Queryable ‘s public static

  • 0

How can I find a generic overloaded method? For example, Queryable‘s

public static IQueryable<TResult> Select<TSource , TResult> ( this IQueryable<TSource> source , Expression<Func<TSource , int , TResult>> selector );

I’ve looked for existing solutions, and they’re either not generic enough (are based on the method’s parameters count, etc.), need more parameters than I have (require generic type definitions or arguments), or just plain wrong (don’t account for nested generics, etc.)

I have the defining class type — Type type, the method name — string name and the array of the parameter types (not the generic definitions) — Type[] types.

So far it seems I have to map each of prospective method’s .GetGenericArguments() to a specific type by comparing the (generic type tree?) of the method’s .GetParameters ().Select (p=>p.ParameterType) with the corresponding item in the types array, thus deducing the generic arguments of the method, so I can .MakeGenericMethod it.

This seems a bit too complicated for the task, so maybe I’m overthinking the whole thing.

Any 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-13T07:50:56+00:00Added an answer on May 13, 2026 at 7:50 am

    Ok, so I just coded this, which is essentially a manual type inference, and I think should do what I need.

    public static class TypeExtensions {
    
        public static Type GetTypeDefinition ( this Type type ) {
            return type.IsGenericType ? type.GetGenericTypeDefinition () : type;
        }
    
        public static IEnumerable<Type> GetImproperComposingTypes ( this Type type ) {
            yield return type.GetTypeDefinition ();
            if ( type.IsGenericType ) {
                foreach ( var argumentType in type.GetGenericArguments () ) {
                    foreach ( var t in argumentType.GetImproperComposingTypes () ) yield return t;
                }
            }
        }
    
        private static Dictionary<Type , Type> GetInferenceMap ( ParameterInfo[] parameters , Type[] types ) {
            var genericArgumentsMap = new Dictionary<Type , Type> ();
            var match = parameters.All ( parameter => parameter.ParameterType.GetImproperComposingTypes ().Zip ( types[parameter.Position].GetImproperComposingTypes () ).All ( a => {
                if ( !a.Item1.IsGenericParameter ) return a.Item1 == a.Item2;
                if ( genericArgumentsMap.ContainsKey ( a.Item1 ) ) return genericArgumentsMap[a.Item1] == a.Item2;
                genericArgumentsMap[a.Item1] = a.Item2;
                return true;
            } ) );
            return match ? genericArgumentsMap : null;
        }
    
        public static MethodInfo MakeGenericMethod ( this Type type , string name , Type[] types ) {
            var methods = from method in type.GetMethods ()
                          where method.Name == name
                          let parameters = method.GetParameters ()
                          where parameters.Length == types.Length
                          let genericArgumentsMap = GetInferenceMap ( parameters , types )
                          where genericArgumentsMap != null
                          where method.GetGenericArguments ().Length == genericArgumentsMap.Keys.Count ()
                          select new {
                              method ,
                              genericArgumentsMap
                          };
            return methods.Select ( m => m.method.IsGenericMethodDefinition ? m.method.MakeGenericMethod ( m.method.GetGenericArguments ().Map ( m.genericArgumentsMap ).ToArray () ) : m.method ).SingleOrDefault ();
        }
    
    }
    

    So given

    public class Foos {
        public void Foo<T1 , T2 , T3> ( int a , T1 b , IEnumerable<T2> c , Expression<Func<T1 , T3 , string>> d ) {
        }
        public void Foo<T1 , T2 , T3> ( int a , T1 b , IEnumerable<T2> c , Expression<Func<T1 , T3 , int>> d ) {
        }
        public void Foo () {
        }
        public void Foo ( string s ) {
        }
    }
    

    I can pick the method I want:

    var method = typeof ( Foos ).MakeGenericMethod ( "Foo" , new[] { typeof ( int ) , typeof ( DateTime ) , typeof ( IEnumerable<string> ) , typeof ( Expression<Func<DateTime , double , int>> ) } );
    method.Invoke ( new Foos () , new object[] { 1 , DateTime.Now , null , null } );
    
    var method = typeof ( Foos ).MakeGenericMethod ( "Foo" , Type.EmptyTypes );
    method.Invoke ( new Foos () , new object[] { } );
    
    var method = typeof ( Foos ).MakeGenericMethod ( "Foo" , new[] { typeof ( string ) } );
    method.Invoke ( new Foos () , new object[] { "zozo" } );
    

    This seem to support non-generic methods, but doesn’t support explicit generic arguments (obviously), and maybe needs some work, but that’s the core of it.

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

Sidebar

Ask A Question

Stats

  • Questions 315k
  • Answers 315k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer It is hard to make sense of your question. However,… May 13, 2026 at 11:23 pm
  • Editorial Team
    Editorial Team added an answer Templates are compile time constructs, threads are run-time ones -… May 13, 2026 at 11:23 pm
  • Editorial Team
    Editorial Team added an answer You could define the color first: color <- rep("black", length(bin))… May 13, 2026 at 11:23 pm

Related Questions

I've run into an issue in Java's Generics in which the same code will
i just landed on SunOS: $ uname -a SunOS sunfi95 5.9 Generic_122300-13 sun4u sparc
How can i make the following class as general as possible (for maximum reuse)
The name of a temporary table such as #t1 can be determined using select
I have a TextBox in a Panel on an aspx page. I need to

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.