Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3847482
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T16:33:30+00:00 2026-05-19T16:33:30+00:00

I was wondering if a C# extension method of some class could act as

  • 0

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,);
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T16:33:31+00:00Added an answer on May 19, 2026 at 4:33 pm

    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)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following extension method: public static void ThrowIfArgumentIsNull<T>(this T value, string argument)
I have extension method that does something like this public static void DoStuff(this ObjectContext
Wondering if someone could help me. I have next to no knowledge with Ajax,
I have a method that starts like this: public static UnboundTag ResolveTag(Type bindingType, string
I have the following extension method which asserts that a property (Id) contains a
I'm wondering if it's possible to write a passthrough extension method for IQueryable which
I am wondering if I can use extension method or other techniques to extend
I'm wondering what the general recommendation would be (attribute, interface, abstract class, or combination
I was wondering, if there is an extension method that allow me to iterate
In our ASP.NET MVC project, we have an HtmlHelper extension method to generate a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.