如何确定一种方法“是否是类型的扩展方法?”
我想编写一个名为 IsExtensionMethod 的方法,其签名是:
public bool IsExtensionMethod(object anObject, string method)
{
// if method was an extension for anObject return true else false
}
我该怎么做?
我尝试了以下代码,但在某些情况下它的返回值不正确:
public bool IsExtensionMethod(object anObject, string method)
{
return anObject.GetType().GetMethods().Where(item => item.Name == method).Count() == 0;
}
I want to write a method named IsExtensionMethod that its signature is :
public bool IsExtensionMethod(object anObject, string method)
{
// if method was an extension for anObject return true else false
}
How can i do it?
I tried for following code but in some cases it's return value is incorrect:
public bool IsExtensionMethod(object anObject, string method)
{
return anObject.GetType().GetMethods().Where(item => item.Name == method).Count() == 0;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
编译器在编译扩展方法时会向其添加一个
CompilerServices.ExtensionAttribute
属性。寻找这个属性。http://www.hanselman.com/blog/HowDoExtensionMethodsWorkAndWhyWasANewCLRNotRequired.aspx
The compiler adds a
CompilerServices.ExtensionAttribute
attribute to extension methods when it compiles them. Look for this attribute.http://www.hanselman.com/blog/HowDoExtensionMethodsWorkAndWhyWasANewCLRNotRequired.aspx