如何使用 GetMethod 进行静态扩展方法
我有一个扩展方法:
public static class StringEx
{
public static bool Like(this string a, string b)
{
return a.ToLower().Contains(b.ToLower());
}
}
如何通过 GetMethod 使用我的参数正确反映它?我尝试过但没有成功(关于静态方法有一个例外):
var like = typeof(StringEx).GetMethod("Like", new[] {typeof(string), typeof(string)});
comparer = Expression.Call(prop, like, value);
I've got an extension method:
public static class StringEx
{
public static bool Like(this string a, string b)
{
return a.ToLower().Contains(b.ToLower());
}
}
How to reflect it properly via GetMethod with my parameters? I've tried this with no success (Got an exception about static method):
var like = typeof(StringEx).GetMethod("Like", new[] {typeof(string), typeof(string)});
comparer = Expression.Call(prop, like, value);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
使用此扩展方法来获取其他扩展方法:
像这样使用它:
Use this extension method to get other extension methods:
Use it like this:
您应该使用带有 BindingAttr 参数的 GetMethod 的另一个重载:
You should use another overload of GetMethod with BindingAttr parameter:
假设你有一个扩展方法:
你需要这样的东西:
已测试;)...
Assuming you've an extension method:
You need something like this:
Tested ;)...
您可以像访问任何静态方法一样访问此方法:
You could to access this method as you would do with any static method: