Expose GetMethod and GetConstructor extensions with both BindingFlags and Type[]
System.Reflection.TypeExtensions is good for porting existing .NET Framework code across, however it is missing an overload with a combination of BindingFlags and Type[] for both GetMethod and GetConstructor.
My use case is getting a private constructor when there are multiple constructors:
type.GetConstructor(
BindingFlags.Instance | BindingFlags.NonPublic,
null, // binder
new[] { typeof(object), ... },
null // modifiers
);
I assume these weren't exposed in corefx because you didn't want to provide the overloads with a Binder parameter, so something like these would be optimal however would be new overloads not available in .NET Framework:
public static MethodInfo GetMethod(this Type type, string name, BindingFlags bindingAttr, Type[] types)
public static ConstructorInfo GetConstructor(this Type type, BindingFlags bindingAttr, Type[] types)
I've looked at using Linq over the TypeInfo but I'd have to check each property rather than use BindingFlags, and TypeInfo isn't available in .NET 3.5/4.0 so I can't just change to one or the other. For now I've emulated the old .NET Framework signature using type.GetConstructors(bindingAttr).SingleOrDefault(...), it is ugly and likely heaps slower than the .NET Framework reflection call.
I'm keen to hear if you have any intention of providing this API, otherwise I'll probably change all our code using the API to a shim extension method something like type.GetPrivateConstructor(new[] { ... }) with conditionally compiled implementation.
@terrajobst @weshaggard PTAL
@pallavit more APIs in the reflection area we should expose.
@jonorossi we are going to take a deeper look at the entire reflection surface area and attempt to add most of the APIs that exist on System.Type to System.Reflection.TypeInfo. To help eliminate most of this problem. It will still require a call from type.GetTypeInfo() but beyond that it should support most things.
I've also run into an issue where I need the signature GetMethod(string name, BindingFlags bindingAttr, Type[] types). Without BindingFlags, I'm not able to find private methods. I really hope this will be implemented soon.
@henningst there are currently extensions in System.Reflection.TypeExtensions that help file some of these gaps today (https://github.com/dotnet/corefx/blob/master/src/System.Reflection.TypeExtensions/src/System/Reflection/TypeExtensions.CoreCLR.cs#L131), try those to help unblock yourself.
Addressed via dotnet/corefx#5381