扩展法和正则法 =>错误?
我有两种相同的方法。 一个是
public void ExtendFrameIntoClientArea(Window w, int amount)
{
if (internals.DwmIsCompositionEnabled())
{
WindowInteropHelper wi = new WindowInteropHelper(w);
internals.DwmExtendFrameIntoClientArea(wi.Handle, new internals.MARGINS(amount));
}
}
,另一个是
public void ExtendFrameIntoClientArea(this Window w,int amount)
{
this.ExtendFrameIntoClientArea(w, amount);
}
其中一个是扩展方法,另一个不是。然而,这会导致错误“此调用不明确”
我该如何解决这个问题?
I have two methods that are identical.
one is
public void ExtendFrameIntoClientArea(Window w, int amount)
{
if (internals.DwmIsCompositionEnabled())
{
WindowInteropHelper wi = new WindowInteropHelper(w);
internals.DwmExtendFrameIntoClientArea(wi.Handle, new internals.MARGINS(amount));
}
}
and the other is
public void ExtendFrameIntoClientArea(this Window w,int amount)
{
this.ExtendFrameIntoClientArea(w, amount);
}
One of them is an extension method and the other one is not. This however, results in an error "This call is ambiguous"
How would I work around this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
扩展方法应该是静态的。
扩展方法应该有一个静态类和一个静态方法。
Extension methods should be static.
Extension methods should have a static class and a static method.
根据 C# 版本 3.0 规范,< a href="http://msdn.microsoft.com/en-us/library/ms364047%28VS.80%29.aspx#cs3spec_topic3" rel="nofollow">搜索顺序是:
那么您如何声明您的方法以及在哪里声明?
According to C# Version 3.0 Specification, the search order is:
So how you declared your methods and where?
我认为该错误不是由扩展方法引起的。
首先,扩展方法
(顺便说一下,您错过了 static 修饰符)与
类
Window
中声明的实例方法会产生歧义,但无论如何,与实例方法则不会产生歧义。它在哪个类中声明。此外 - 据我记得 - 实例方法优先于扩展方法 - 因此它们永远不应该与扩展方法产生歧义。我建议再次查看错误消息并验证您正在查看正确的方法。
I think the error is not caused by the extension method.
First, an extension method
(by the way, you missed the
static
modifier) would be ambiguous with a instance methoddeclared in the class
Window
but not with a instance methodno matter in what class it is declared. Further - as far as I remember - instance methods take precedence over extension method - so they should never be ambiguous with extension methods. I suggest to have a look at the error message again and verify that you are looking at the right methods.