proposal: support some way of sending a method group to something #3730
Replies: 6 comments
-
Why not make it accept a |
Beta Was this translation helpful? Give feedback.
-
You would need to target type your methods to a specific delegate type first (e.g. |
Beta Was this translation helpful? Give feedback.
-
exactly - I would love it if method groups could implicitly float into delegates - that would be awesome!!! |
Beta Was this translation helpful? Give feedback.
-
The problem is that unfortunately in c# there is no canonical set of delegate types. |
Beta Was this translation helpful? Give feedback.
-
yeah - that's why I reckon the easiest implementation, without changing anything else too much, is to actual define a new, special, "Methodgroup" type - that reflects all the individual overloads, and that any method group can be implicitly converted into - because it doesn't have any effect on anything beyond the special handling of this type. it's almost like the [callermethodname] attributes - in that the compiler would just have to do something special when it encounters an invocation of it. (and of course, it could contain of have a way of producing a enumerable of delegates - because each particular instance in a method group is trivial to turn into a delegate) |
Beta Was this translation helpful? Give feedback.
-
Current workaround: public class MethodGroup {
public readonly MethodInfo[] Methods;
public readonly object Instance;
public MethodGroup ( object instance, params MethodInfo[] methods ) : this( methods ) {
Instance = instance;
}
public MethodGroup ( params MethodInfo[] methods ) {
Methods = methods;
}
public object Invoke ( params object[] parameters ) {
foreach ( var i in Methods ) {
var p = i.GetParameters();
if ( p.Length != parameters.Length ) continue;
bool ok = true;
for ( int n = 0; n < parameters.Length; n++ ) {
if ( !p[ n ].ParameterType.IsAssignableFrom( parameters[ n ].GetType() ) ) {
ok = false;
break;
}
}
if ( ok ) {
return i.Invoke( Instance, parameters );
}
}
throw new InvalidOperationException();
}
public static MethodGroup FromType<T> ( string name )
=> new MethodGroup(
typeof( T ).GetMethods( BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static )
.Where( x => x.Name == name ).ToArray()
);
public static MethodGroup FromDelegate ( Action action ) // do the same for all Action<T1,T2...> and Func<T1,T2...>
=> new MethodGroup( null, new[] { action.Method } );
}
public static class MethodGroupExtensions {
public static MethodGroup GetMethodGroup<T> ( this T instance, string name )
=> new MethodGroup( instance,
typeof( T ).GetMethods( BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance )
.Where( x => x.Name == name ).ToArray()
);
} You definitely can also create |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I encounter this a lot - basically - I use reflection a lot and have discovered that it's actually completely impossible to create a function that just takes any method. I understand why - Eric L explained it at length, but it's still something I come across on a monthly basis.
Lets take the most recent example. System.commandline is pretty cool. System.commandline.dragonfruit is even cooler. However - I discover I can't use it for two reasons ... a) the auto generated main conflicts with the one that xunit produces - so you can't have them in the same exe and b) I want lots of commands - like git, where you have mainCommand command arguments...
so - I write my own thing - that just wraps system.commandline and does what dragonfruit does - gets all the help text from the xmldoc comments etc... and it works great... However, what I want is
but of course, it's impossible to create a signature for Expose - because you can't take make a function that accepts an arbitrary method group, because the method choice is only resolved after the function resolution (even if there is only one method in the group!!!!) :(.
so I end up having to make names unique, and have expose on startup look at the stack trace - get the assembly from it, scan all types in the assembly, then all static methods on the types, and have this crappy signature:
but there are many many other times when you want to call a method by reflection for some reason - and there's just no way to provide the method. What I'd really like is to be able to something like:
ie - just make it possible, even if difficult, to pass a method group to something
Beta Was this translation helpful? Give feedback.
All reactions