Skip to content

Instantly share code, notes, and snippets.

@jonnii
Created February 5, 2013 14:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jonnii/4714812 to your computer and use it in GitHub Desktop.
Save jonnii/4714812 to your computer and use it in GitHub Desktop.
[TestFixture]
public class Example
{
public class Bus
{
public void SubscribeAsync<T>(string subscriptionId, Func<T, Task> onMessage) { }
}
public static MethodInfo GetMethod<T>(Expression<Action<T>> methodSelector)
{
var body = (MethodCallExpression)methodSelector.Body;
return body.Method;
}
[Test]
public void Should()
{
var bus = new Bus();
MethodInfo info = bus.GetType().GetMethods()
.Where(m => m.Name == "SubscribeAsync")
.Select(m => new { Method = m, Params = m.GetParameters() })
.Single(m => m.Params.Length == 2
&& m.Params[0].ParameterType == typeof(string)
//&& m.Params[1].ParameterType.GetGenericTypeDefinition() == typeof(Func<>) //<-- not sure how to pull this off?
).Method;
Console.WriteLine(info.Name);
var tree = GetMethod<Bus>(b => b.SubscribeAsync((string)null, (Func<int, Task>)null));
Console.WriteLine(tree.Name);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment