Skip to content

[API Proposal]: Add overload of Type.GetMethod that takes a name, generic parameter count, binding flags and parameter types #90995

Closed

Description

@Neme12

Background and motivation

In #42753, an overload of Type.GetMethod was added that takes a name, binding flags, and paramater types, to simplify usage when people don't care about the binder and paramater modifiers (which as I understand is there only for COM), which basically nobody passes in ever. However no equivalent overload was added that also takes genericParameterCount at the beginning. This is the overload I always use, even when the method isn't generic and I pass in 0, to be as specific as possible and make sure that my code still works even in case there is a generic overload added in the future. It seems that all the other overloads have an equivalent that takes genericParameterCount at the beginning, so this would just square off the feature in my opinion.

API Proposal

 namespace System;

 public abstract partial class Type
 {
     public MethodInfo? GetMethod(string name, int genericParameterCount, Type[] types);
     public MethodInfo? GetMethod(string name, int genericParameterCount, Type[] types, ParameterModifier[]? modifiers);
+    public MethodInfo? GetMethod(string name, int genericParameterCount, BindingFlags bindingAttr, Type[] types);
     public MethodInfo? GetMethod(string name, int genericParameterCount, BindingFlags bindingAttr, Binder? binder, Type[] types, ParameterModifier[]? modifiers);
     public MethodInfo? GetMethod(string name, int genericParameterCount, BindingFlags bindingAttr, Binder? binder, CallingConventions callConvention, Type[] types, ParameterModifier[]? modifiers);
     public MethodInfo? GetMethod(string name);
     public MethodInfo? GetMethod(string name, Type[] types);
     public MethodInfo? GetMethod(string name, Type[] types, ParameterModifier[]? modifiers);
     public MethodInfo? GetMethod(string name, BindingFlags bindingAttr);
     public MethodInfo? GetMethod(string name, BindingFlags bindingAttr, Type[] types);
     public MethodInfo? GetMethod(string name, BindingFlags bindingAttr, Binder? binder, Type[] types, ParameterModifier[]? modifiers);
     public MethodInfo? GetMethod(string name, BindingFlags bindingAttr, Binder? binder, CallingConventions callConvention, Type[] types, ParameterModifier[]? modifiers);
 }

API Usage

using System.Reflection;

var method = typeof(C).GetMethod("M", 0, BindingFlags.Public | BindingFlags.Instance, Array.Empty<Type>());
// or "Type.EmptyTypes" instead of "Array.Empty<Type>()"

class C
{
    public void M();
}

Alternative Designs

Original issue alternative (not proposed): optionally, to square off the set of overloads completely, with complete parity between the ones that take genericParameterCount and the ones that don't (7 overloads for each):

 namespace System;

 public abstract partial class Type
 {
+    public MethodInfo? GetMethod(string name, int genericParameterCount);
     public MethodInfo? GetMethod(string name, int genericParameterCount, Type[] types);
     public MethodInfo? GetMethod(string name, int genericParameterCount, Type[] types, ParameterModifier[]? modifiers);
+    public MethodInfo? GetMethod(string name, int genericParameterCount, BindingFlags bindingAttr);
+    public MethodInfo? GetMethod(string name, int genericParameterCount, BindingFlags bindingAttr, Type[] types);
     public MethodInfo? GetMethod(string name, int genericParameterCount, BindingFlags bindingAttr, Binder? binder, Type[] types, ParameterModifier[]? modifiers);
     public MethodInfo? GetMethod(string name, int genericParameterCount, BindingFlags bindingAttr, Binder? binder, CallingConventions callConvention, Type[] types, ParameterModifier[]? modifiers);
     public MethodInfo? GetMethod(string name);
     public MethodInfo? GetMethod(string name, Type[] types);
     public MethodInfo? GetMethod(string name, Type[] types, ParameterModifier[]? modifiers);
     public MethodInfo? GetMethod(string name, BindingFlags bindingAttr);
     public MethodInfo? GetMethod(string name, BindingFlags bindingAttr, Type[] types);
     public MethodInfo? GetMethod(string name, BindingFlags bindingAttr, Binder? binder, Type[] types, ParameterModifier[]? modifiers);
     public MethodInfo? GetMethod(string name, BindingFlags bindingAttr, Binder? binder, CallingConventions callConvention, Type[] types, ParameterModifier[]? modifiers);
 }

Risks

No response

Activity

  1. added
    api-suggestion
    on Aug 23, 2023
  2. ghost added
    untriaged
    on Aug 23, 2023
  3. changed the title [-][API Proposal]: [/-] [+][API Proposal]: Add oveerload of `Type.GetMethod` that takes a name, generic parameter count, binding flags and parameter types[/+] on Aug 23, 2023
  4. changed the title [-][API Proposal]: Add oveerload of `Type.GetMethod` that takes a name, generic parameter count, binding flags and parameter types[/-] [+][API Proposal]: Add overload of `Type.GetMethod` that takes a name, generic parameter count, binding flags and parameter types[/+] on Aug 23, 2023
  5. steveharter commented on Aug 24, 2023

    @steveharter
    Contributor

    However no equivalent overload was added that also takes genericParameterCount at the beginning.

    The original request would help the common cases:

    public MethodInfo? GetMethod(string name, int genericParameterCount, BindingFlags bindingAttr, Type[] types);

    However, I'm not so sure about the other 2 since they remove the Type[] parameter which is a good practice for open-ended method lookup to disambiguate similar methods.

  6. removed
    untriaged
    on Aug 24, 2023
  7. added this to the Future milestone on Aug 24, 2023
  8. Neme12 commented on Aug 24, 2023

    @Neme12
    Author

    Right, I'm only interested in that one, I just added the proposal for the rest of them only as an idea if parity and consistency is desired.

  9. steveharter commented on Nov 2, 2023

    @steveharter
    Contributor

    I think it makes sense to add this to v9:

    public MethodInfo? GetMethod(string name, int genericParameterCount, BindingFlags bindingAttr, Type[] types);
  10. added
    api-ready-for-review
    and removed
    api-suggestion
    on Nov 2, 2023
  11. modified the milestones: Future, 9.0.0 on Nov 2, 2023
  12. added
    help wanted
    on Nov 2, 2023
  13. TheMaximum commented on Nov 10, 2023

    @TheMaximum
    Contributor

    I'd be interested in taking this up if it gets through the API review :)

  14. bartonjs commented on Nov 14, 2023

    @bartonjs
    Member

    Video

    Looks good as proposed.

     namespace System;
    
     public abstract partial class Type
     {
         public MethodInfo? GetMethod(string name, int genericParameterCount, Type[] types);
         public MethodInfo? GetMethod(string name, int genericParameterCount, Type[] types, ParameterModifier[]? modifiers);
    +    public MethodInfo? GetMethod(string name, int genericParameterCount, BindingFlags bindingAttr, Type[] types);
         public MethodInfo? GetMethod(string name, int genericParameterCount, BindingFlags bindingAttr, Binder? binder, Type[] types, ParameterModifier[]? modifiers);
         public MethodInfo? GetMethod(string name, int genericParameterCount, BindingFlags bindingAttr, Binder? binder, CallingConventions callConvention, Type[] types, ParameterModifier[]? modifiers);
         public MethodInfo? GetMethod(string name);
         public MethodInfo? GetMethod(string name, Type[] types);
         public MethodInfo? GetMethod(string name, Type[] types, ParameterModifier[]? modifiers);
         public MethodInfo? GetMethod(string name, BindingFlags bindingAttr);
         public MethodInfo? GetMethod(string name, BindingFlags bindingAttr, Type[] types);
         public MethodInfo? GetMethod(string name, BindingFlags bindingAttr, Binder? binder, Type[] types, ParameterModifier[]? modifiers);
         public MethodInfo? GetMethod(string name, BindingFlags bindingAttr, Binder? binder, CallingConventions callConvention, Type[] types, ParameterModifier[]? modifiers);
     }
  15. added
    api-approved
    and removed
    api-ready-for-review
    on Nov 14, 2023
  16. ghost added
    in-pr
    on Nov 17, 2023
  17. ghost removed
    in-pr
    on Nov 21, 2023
  18. locked and limited conversation to collaborators on Dec 22, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    api-approvedarea-System.Reflectionhelp wanted

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions