Created
February 5, 2013 14:36
-
-
Save jonnii/4714812 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[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