C# 扩展方法作为接口实现
我想知道某个类的 C# 扩展方法是否可以充当接口的实现? 我有什么:
一个 iterface:
public interface IEventHandler
{
void Notify(SEvent ev, IEventEmmiter source);
}
一个实现它的类:
class Sim : IEventHandler
{
/*public void Notify(SEvent ev, IEventEmmiter source)
{
Console.WriteLine("Got notified: " + ev.Name);
}*/
}
一个包含扩展方法的类:
public static class ReflectiveEventDispatcher
{
public static void Notify(this IEventHandler handler, SEvent ev)
{
if (handler.GetType().GetMethod("Handle" + ev.Name) != null)
{
// C# WTF?
object[] prms = new object[0];
prms[0] = ev;
handler.GetType().GetMethod("Handle" + ev.Name).Invoke(handler, prms);
}
else
{
throw new System.NotImplementedException("This object doesn't have appropriate handler methods for event " + ev.Name);
}
}
}
现在,我想要具有 IEventHandler 接口的各种类,并且接口的实现应该由扩展方法来实现。
如果这是不可能的,是否可以显式定义 Notify,然后将调用转发到扩展方法?
上面的代码基本上是一个多重继承 hack。是否可以通过任何(其他)方式来模拟这种行为?
(我希望这是有道理的,我已经习惯了 Ruby,这让我真的很难过。哦,我怎么想你,我亲爱的 mixins...)
更新
呼叫转发很好地解决了这个问题:
public void Notify(SEvent ev)
{
ReflectiveEventDispatcher.Notify(this, ev,);
}
I was wondering if a C# extension method of some class could act as an implementation of interface?
What do I have:
An iterface:
public interface IEventHandler
{
void Notify(SEvent ev, IEventEmmiter source);
}
A class that implements it:
class Sim : IEventHandler
{
/*public void Notify(SEvent ev, IEventEmmiter source)
{
Console.WriteLine("Got notified: " + ev.Name);
}*/
}
And a class that contains the extension method:
public static class ReflectiveEventDispatcher
{
public static void Notify(this IEventHandler handler, SEvent ev)
{
if (handler.GetType().GetMethod("Handle" + ev.Name) != null)
{
// C# WTF?
object[] prms = new object[0];
prms[0] = ev;
handler.GetType().GetMethod("Handle" + ev.Name).Invoke(handler, prms);
}
else
{
throw new System.NotImplementedException("This object doesn't have appropriate handler methods for event " + ev.Name);
}
}
}
Now, I want to have various classes with IEventHandler interface and the interfaces' implementation should be fulfilled by the extension method.
If that's not possible, is it possible to define the Notify explicitly and then just forward the call to the extension method?
The code above is basically a multiple inheritance hack. Is it possible to emulate this behaviour by any (other) means?
(I hope this makes sense, I'm used to Ruby and this is giving me really hard time. Oh, how do I miss you, my dear mixins...)
Update
Call forwarding solved it pretty well:
public void Notify(SEvent ev)
{
ReflectiveEventDispatcher.Notify(this, ev,);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
不,扩展方法不能充当接口的实现。扩展方法只是静态方法的语法糖,以该类的实例作为第一个参数,因此不是特定类的成员,而这是实现接口的要求。
No, Extension methods can not act as implementation for an interface. Extensions methods are just syntactic sugar for a static method taking an instance of that class as the first parameter, thus not being a member of the specific class, which is a requirement for implementing an interface.
这不能按照您描述的方式完成:C# 方法调用解析将始终选择方法的具体实现,而不是扩展方法。
但是,我相信如果您不在接口中定义该方法,而只是将其作为不同名称空间中的扩展方法,那么您可以在某种程度上得到您想要的东西。当然,这是“静态”的,因为调用的方法仍然会在编译时被选择。
实际上,然后您可以选择通过正确的“using”指令调用的方法(类似于 LINQ 的工作方式)
this can not be done the way you describe it: C# method call resolution will always choose the concrete implementation of your method, never the extension method.
However, I believe you can -somewhat- get what you want if you DON'T define the method in the interface, but simply as extension methods in different namespaces. Of course, this is "static", in that the method called will still be selected at compile time.
Practically speaking, you then select the method called through the right "using" directive (similar to how LINQ works)
这是不可能的。
接口方法必须由实现类型本身来实现。
扩展方法是一个纯粹的语言级概念(语法糖),并没有什么帮助。 CLR 本身完全不知道扩展方法,
This is not possible.
Interface methods must be implemented by the implementing type itself.
Extension methods are a purely language-level concept (syntax sugar), and won't help. The CLR itself is completely unaware of extension methods,