-
Notifications
You must be signed in to change notification settings - Fork 4.1k
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 memberof operator #1653
Comments
Eric Lippert has a blog post that explains why this feature is quite problematic. Probably the biggest problem is choosing which overload of a method to return. |
Granted it would be entirely new syntax, but I don't understand why that can't be solved through explicitly specifying the type names as arguments: public void Foo(int value) { }
public void Bar(int value) { }
public void Bar(object value) { }
MethodBase fooMethod = methodof(Foo); // legal, only one Foo
MethodBase barMethod1 = methodof(Bar); // compiler error, ambiguous
MethodBase barMethod2 = methodof(Bar(int)); // resolves to Bar(int) Supporting variance would be nice but I don't think it would be necessary: MethodBase barMethod3 = methodof(Bar(string)); // compiler error, or resolve to Bar(object)? Also, in order to support events or properties using this kind of syntax would require changes to the CLR, assuming that it uses a similar mechanism to I do like the addition here of a syntax to explicitly reference property/event accessor methods. |
Appreciate the support for this :-) Java doesn't have a ldtoken and so the way Java "efficiently" supports their typeof equivalent (Type.class) is to use reflection and then cache the Class object as a static field of the calling class. This approach could be used for memberof on methods, fields and properties (caching the result of Type.GetProperty on the calling class). |
@svick The post by Eric is substantially outdated. During the discussion for the
The last two items don't directly prevent |
Are you referring to that syntax that involved either providing actual parameter values or I do think that |
Manually resolving MethodInfo using reflection is also prone to runtime errors if the APIs change as well. For example, a naive call to Type.GetMethods().First(c => c.Name == "Foo") will break if overloads to Foo are added later on. Using memberof(Type.Foo()) is much safer. Trying to exactly resolve a method with the signature |
Hmm, that sounds like something that could be solved by a library that takes a delegate type. Something like: delegate void FooDelegate<T>(string s, out T x) where T : struct;
…
var mi = type.GetMethod("Foo", typeof(FooDelegate<>)); It feels weird to create a delegate type just to describe the signature of a method, but otherwise I think it should work pretty well. Maybe something like this could be used for |
That's actually not an unreasonable suggestion. Could get away with Func<...> and Action<...> most of the time too (just not with ref/out arguments). |
@svick Such a method would be very helpful as an addition to the BCL. The state of reflecting types/methods is fairly poor as no real improvements have been made to the API since 1.x. That method would still be slower than a real I've proposed other helper methods to be added to the BCL for generic type/method reflection based on extension methods that I've written and have been using very successfully for some years: Proposal: Additional methods to aid in reflection of generic types |
Also reflection is used quite rarely in comparison to other features. Maybe fixing the existing reflection API and object model to make it more usable would make more sense? |
The purpose is to get the member of the provided token, so |
What does this mean? It reads like: "here is a token x, give me a member that is a part of it". Which is not what you want. You need something that reads like: "give me a member x". Maybe what you want is typeof(int); //Get an instance of Type.
infoof(int); //Get an instance of TypeInfo.
infoof(int.parse); //Get an instance of MethodInfo. |
The name is a lot less relevant to me than the capability. I often throw around My issue with |
Any news about this request? Because many times I've this need. The compiler can work in the same way it compiles the Expression, using ldtoken MethodBase::GetMethodFromHandle. static void Main(string[] args)
{
Expression<Action<string>> t = s => Test(s);
MethodBase method = ((MethodCallExpression)t.Body).Method;
}
private static void Test(string s)
{
} But I don't like it so much |
For now Ricciolo's excellent suggestion is what I'm doing
|
With regards to naming, Although System.Type extends MemberInfo so theoretically |
We are now taking language feature discussion in other repositories:
Features that are under active design or development, or which are "championed" by someone on the language design team, have already been moved either as issues or as checked-in design documents. For example, the proposal in this repo "Proposal: Partial interface implementation a.k.a. Traits" (issue 16139 and a few other issues that request the same thing) are now tracked by the language team at issue 52 in https://github.com/dotnet/csharplang/issues, and there is a draft spec at https://github.com/dotnet/csharplang/blob/master/proposals/default-interface-methods.md and further discussion at issue 288 in https://github.com/dotnet/csharplang/issues. Prototyping of the compiler portion of language features is still tracked here; see, for example, https://github.com/dotnet/roslyn/tree/features/DefaultInterfaceImplementation and issue 17952. In order to facilitate that transition, we have started closing language design discussions from the roslyn repo with a note briefly explaining why. When we are aware of an existing discussion for the feature already in the new repo, we are adding a link to that. But we're not adding new issues to the new repos for existing discussions in this repo that the language design team does not currently envision taking on. Our intent is to eventually close the language design issues in the Roslyn repo and encourage discussion in one of the new repos instead. Our intent is not to shut down discussion on language design - you can still continue discussion on the closed issues if you want - but rather we would like to encourage people to move discussion to where we are more likely to be paying attention (the new repo), or to abandon discussions that are no longer of interest to you. If you happen to notice that one of the closed issues has a relevant issue in the new repo, and we have not added a link to the new issue, we would appreciate you providing a link from the old to the new discussion. That way people who are still interested in the discussion can start paying attention to the new issue. Also, we'd welcome any ideas you might have on how we could better manage the transition. Comments and discussion about closing and/or moving issues should be directed to #18002. Comments and discussion about this issue can take place here or on an issue in the relevant repo. I am not moving this particular issue because I don't have confidence that the LDM would likely consider doing this. |
The idea of the memberof operator would be the equivalent to the typeof operator but would operate on fields, methods and properties. This is useful when working with Expression trees and is distinct from the proposed nameof operator because it returns System.Reflection.MemberInfo objects rather than strings.
Example:
The text was updated successfully, but these errors were encountered: