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

Make it easy to obtain the MethodInfo for a method #31427

Closed
WillSullivan opened this issue Nov 28, 2018 · 2 comments
Closed

Make it easy to obtain the MethodInfo for a method #31427

WillSullivan opened this issue Nov 28, 2018 · 2 comments

Comments

@WillSullivan
Copy link

I'm knee deep in Expression land (the mascots are obscure, for some reason) and I'm finding I have to get quite a few MethodInfos for a lot of Expression.Calls, and there is just not any easy way to obtain them for complex and generic methods.

Who doesn't love reflecting over hoary old chestnuts like

method = typeof(AppDomain).GetMethod(
     nameof(AppDomain.CreateDomain), 
     BindingFlags.Public | BindingFlags.Static, 
     null, 
     new[] { typeof(string), typeof(Evidence), typeof(AppDomainSetup), typeof(PermissionSet), typeof(StrongName[]) }, 
     null);

And this doesn't look like a dirty filthy hack at all (and courtesy of Jon Skeet, no less)

method = typeof(Enumerable).GetMethods()
    .Where(m => m.Name == nameof(Enumerable.Contains))
    .Single(m => m.GetParameters().Length == 2)
    .MakeGenericMethod(typeof(int));

You have to admit this is pretty awful. Could we have a new operator to make this a little better?

Proposal: Add a methodof operator

Like typeof, which retrieves the Type of a class definition, add a methodof expression that gives us the MethodInfo for a given method. Using the above as examples, doesn't

methodof(AppDomain.CreateDomain(string, Evidence, AppDomainSetup, PermissionSet, StrongName[]))

and

methodof(Enumerable.Contains(IEnumerable<int>, int)

look almost... nay, absolutely professional? Easier to combine into a single line?

Pros

omg look at what it's replacing. And it would be possible to get intellisense involved, which GetMethod does not provide. And this means you'll know before compile time if you're doing it wrong. With GetMethod I sometimes have to bust out a prototype to play with different BindingFlags until I get something other than null back from the stupid call.

Cons

Somebody's got to code it. Although I can't think of an example I'd bet it might be possible to find a situation where the compiler isn't able to correctly identify the method you want. But if so there's always the fallback option.

I did scan the backlog for similar feature requests, but no luck. Hope I'm not wasting your time on something that's already been proposed.

@sharwell
Copy link
Member

Duplicate of dotnet/csharplang#712

@WillSullivan
Copy link
Author

Aaah, chose the wrong name. Thanks! I'll track that FR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants