Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Type.GetMethod overload that takes Name, BindingFlags, and Parameter Types #42753

Closed
eerhardt opened this issue Sep 25, 2020 · 2 comments · Fixed by #44529
Closed

Add Type.GetMethod overload that takes Name, BindingFlags, and Parameter Types #42753

eerhardt opened this issue Sep 25, 2020 · 2 comments · Fixed by #44529
Assignees
Labels
api-approved API was approved in API review, it can be implemented area-System.Reflection
Milestone

Comments

@eerhardt
Copy link
Member

eerhardt commented Sep 25, 2020

Background and Motivation

When trying to specify which method you want to find in Reflection, often you want to supply a name, BindingFlags, and parameter Types. However, there is no overload today that takes those 3 inputs, and only those 3 inputs. Similarly, when trying to find a constructor, GetConstructor has no overload that just takes BindingFlags and parameter Types.

Proposed API

namespace System
{
    public class Type
    {
        public System.Reflection.MethodInfo? GetMethod(string name) { throw null; }
        public System.Reflection.MethodInfo? GetMethod(string name, int genericParameterCount, System.Reflection.BindingFlags bindingAttr, System.Reflection.Binder? binder, System.Reflection.CallingConventions callConvention, System.Type[] types, System.Reflection.ParameterModifier[]? modifiers) { throw null; }
        public System.Reflection.MethodInfo? GetMethod(string name, int genericParameterCount, System.Reflection.BindingFlags bindingAttr, System.Reflection.Binder? binder, System.Type[] types, System.Reflection.ParameterModifier[]? modifiers) { throw null; }
        public System.Reflection.MethodInfo? GetMethod(string name, int genericParameterCount, System.Type[] types) { throw null; }
        public System.Reflection.MethodInfo? GetMethod(string name, int genericParameterCount, System.Type[] types, System.Reflection.ParameterModifier[]? modifiers) { throw null; }
        public System.Reflection.MethodInfo? GetMethod(string name, System.Reflection.BindingFlags bindingAttr) { throw null; }
        public System.Reflection.MethodInfo? GetMethod(string name, System.Reflection.BindingFlags bindingAttr, System.Reflection.Binder? binder, System.Reflection.CallingConventions callConvention, System.Type[] types, System.Reflection.ParameterModifier[]? modifiers) { throw null; }
        public System.Reflection.MethodInfo? GetMethod(string name, System.Reflection.BindingFlags bindingAttr, System.Reflection.Binder? binder, System.Type[] types, System.Reflection.ParameterModifier[]? modifiers) { throw null; }
        public System.Reflection.MethodInfo? GetMethod(string name, System.Type[] types) { throw null; }
        public System.Reflection.MethodInfo? GetMethod(string name, System.Type[] types, System.Reflection.ParameterModifier[]? modifiers) { throw null; }

+       public System.Reflection.MethodInfo? GetMethod(string name, System.Reflection.BindingFlags bindingAttr, System.Type[] types) { throw null; }

        public System.Reflection.ConstructorInfo? GetConstructor(System.Reflection.BindingFlags bindingAttr, System.Reflection.Binder? binder, System.Reflection.CallingConventions callConvention, System.Type[] types, System.Reflection.ParameterModifier[]? modifiers) { throw null; }
        public System.Reflection.ConstructorInfo? GetConstructor(System.Reflection.BindingFlags bindingAttr, System.Reflection.Binder? binder, System.Type[] types, System.Reflection.ParameterModifier[]? modifiers) { throw null; }
        public System.Reflection.ConstructorInfo? GetConstructor(System.Type[] types) { throw null; }

+        public System.Reflection.ConstructorInfo? GetConstructor(System.Reflection.BindingFlags bindingAttr, System.Type[] types) { throw null; }
     }

Usage Examples

We already have extensions for these today in DataContractSerialization, with it's own implementations:

s_writeEndElementMethod = typeof(XmlWriterDelegator).GetMethod("WriteEndElement", Globals.ScanAllMembers, Array.Empty<Type>());

s_createSerializationExceptionMethod = typeof(XmlObjectSerializerReadContext).GetMethod("CreateSerializationException", Globals.ScanAllMembers, new Type[] { typeof(string) });

ConstructorInfo ci = type.GetConstructor(BindingFlags.Instance | BindingFlags.Public, Array.Empty<Type>())!;

Alternative Designs

We could just tell people to call Type.GetMethod(string, BindingFlags, null, Type[], null) themselves. But that seems too verbose.

Risks

None

cc @steveharter @GrabYourPitchforks

@eerhardt eerhardt added api-suggestion Early API idea and discussion, it is NOT ready for implementation area-System.Reflection labels Sep 25, 2020
@Dotnet-GitSync-Bot Dotnet-GitSync-Bot added the untriaged New issue has not been triaged by the area owner label Sep 25, 2020
@ericstj ericstj removed the untriaged New issue has not been triaged by the area owner label Sep 28, 2020
@ericstj ericstj added this to the 6.0.0 milestone Sep 28, 2020
@ericstj
Copy link
Member

ericstj commented Sep 28, 2020

Seems like a reasonable request.

@eerhardt eerhardt added api-ready-for-review API is ready for review, it is NOT ready for implementation and removed api-suggestion Early API idea and discussion, it is NOT ready for implementation labels Nov 9, 2020
@bartonjs
Copy link
Member

bartonjs commented Nov 10, 2020

Video

Looks good as proposed

namespace System
{
    public partial class Type
    {
        public System.Reflection.MethodInfo? GetMethod(string name, System.Reflection.BindingFlags bindingAttr, System.Type[] types) { throw null; }

        public System.Reflection.ConstructorInfo? GetConstructor(System.Reflection.BindingFlags bindingAttr, System.Type[] types) { throw null; }
     }
}

@bartonjs bartonjs added api-approved API was approved in API review, it can be implemented and removed api-ready-for-review API is ready for review, it is NOT ready for implementation labels Nov 10, 2020
@eerhardt eerhardt self-assigned this Nov 10, 2020
eerhardt added a commit to eerhardt/linker that referenced this issue Nov 10, 2020
With dotnet/runtime#42753, we are adding a new overload to Type.GetConstructor that only takes BindingFlags and Type[].

However, this new overload exposes a bug in the ILLinker where it is using the wrong parameter count to find how many constructor parameters are being used.

Fixing the issue by using the correct parameter count in the switch statement.
vitek-karas pushed a commit to dotnet/linker that referenced this issue Nov 10, 2020
With dotnet/runtime#42753, we are adding a new overload to Type.GetConstructor that only takes BindingFlags and Type[].

However, this new overload exposes a bug in the ILLinker where it is using the wrong parameter count to find how many constructor parameters are being used.

Fixing the issue by using the correct parameter count in the switch statement.
eerhardt added a commit to eerhardt/runtime that referenced this issue Nov 11, 2020
…ter Types

Adding overloads to GetConstructor and GetMethod that don't take a Binder or parameter modifiers.

Fix dotnet#42753
eerhardt added a commit that referenced this issue Nov 11, 2020
…ter Types (#44529)

* Add Type.GetMethod overload that takes Name, BindingFlags, and Parameter Types

Adding overloads to GetConstructor and GetMethod that don't take a Binder or parameter modifiers.

Fix #42753
@ghost ghost locked as resolved and limited conversation to collaborators Dec 12, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
api-approved API was approved in API review, it can be implemented area-System.Reflection
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants