C# extension method as an interface implementation

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,);
    }

C# – using extension methods to provide default interface implementation

I’m just learning about C# extension methods, and was wondering if I can use it to provide a default implementation for an interface. Say: public interface Animal { string MakeSound(); } public static

Implement extension method interface C#

I am newbie to C#, and have a problem with understanding how should I implement an extension method of interface. I didn’t found any material about this issue. From the material I found about general

Ignoring Interface method implementation in C#

Suppose an Interface I has two methods. For example Method1() and Method2(). A class A Implements an Interface I. Is it possible for class A to implement only Method1() and ignore Method2()? I know as

Objective-c -method in implementation only and extension class’s method, what’s the difference

I’m an objective-c beginner, and i saw code like below: DemoController.m: @interface DemoController() -(void)method1; @end @implementation DemoController -(void)method1 { NSLog(@This is method1 in c

Extension method that returns an the correct implementation of an interface?

I am not sure if this is possible and I may need to write extension methods for each implementation. Here is some example code: public interface IBaseService<T> { IUnitOfwork UnitOfWork {get;} }

C#: Interface implementation

I found the following implementation in a code base I am going through Interface IOrder { GetSubmittedOrder(); CreateOrder(); GetExistingOrder(); } Class Order : IOrder { BuildSpecialOrder(); AddSpeci

Interface implementation hiding method

I need advice on interfaces. I have a class called Unit which can attack other units. The attack can be air or ground. I need single method to achieve attacking called attack(). Some units can only at

Separating the Interface and Implementation in C

In separating the interface from implementation in C, where is the right place to include a header file? In the interface file (.h file) or in the implementation file (.c file)? Why? My Sample Code: c

Visual Studio 2010 extension for Go To Interface Implementation? [duplicate]

Possible Duplicate: How do you quickly find the implementation(s) of an interface’s method? I like ReSharper’s Go To Implementation feature but I often keep ReSharper disabled. Is there an equivalen

How to get OPTIONAL interface method implementation in C# like in Objective C 2.0?

Objective C has protocol which is the equivalence of interface in C#. Nevertheless C# enforces to implement all method like in Objective C 1.0. But in Objective C 2.0, it is now possible to mark some

Answers

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,

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.

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)

var sim = new Sim()
((IEventHandler )sim).Notify(ev)