Repository navigation
Description
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
[-][API Proposal]: [/-][+][API Proposal]: Add oveerload of `Type.GetMethod` that takes a name, generic parameter count, binding flags and parameter types[/+][-][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[/+]steveharter commentedon Aug 24, 2023
The original request would help the common cases:
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.Neme12 commentedon Aug 24, 2023
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.
steveharter commentedon Nov 2, 2023
I think it makes sense to add this to v9:
TheMaximum commentedon Nov 10, 2023
I'd be interested in taking this up if it gets through the API review :)
bartonjs commentedon Nov 14, 2023
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); }