c# - Determine if MethodInfo represents a lambda expression -
how 1 determine if methodinfo represents metadata lambda expression?
i think talking anonymous methods.so, can write extension method , check whether name of method contains invalid chars.because compiler generated methods contains invalid chars, can use feature determine whether method anonymous or not:
public static bool isanonymous(this methodinfo method) { var invalidchars = new[] {'<', '>'}; return method.name.any(invalidchars.contains); }
test:
func<int> f = () => 23; console.write(f.method.isanonymous()); // true
more elegant why validating method name using isvalidlanguageindependentidentifier
method, (method this answer):
public static bool isanonymous(this methodinfo method) { return !codegenerator.isvalidlanguageindependentidentifier(method.name); }
remember in order access isvalidlanguageindependentidentifier
method need include system.codedom.compiler
namespace.
Comments
Post a Comment