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 102201
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T00:55:05+00:00 2026-05-11T00:55:05+00:00

C# 3.0 Extension methods add extensions to the base Type making calling that method

  • 0

C# 3.0 Extension methods add extensions to the base Type making calling that method on all instances of that Type legal.

Now, JavaScript I know implements IDispatchEx through which it’s possible to add methods to a specific instance.

So how do I add a set of methods to an ‘instance’ of a C# class? I know this is a Dynamic vs. Static Languages holy war territory. 🙂 Let me clarify my intention is NOT that.

I just want to be able to add a set of events to an interface depending on the class implementing that interface.

I was able to do that using Generics

inteface ISample<T> { T SupportedEvents; }    class Sample : ISample<UIWidgetEvent>   {   }   class Sample2 : ISample<NonVisualUIWidget> { }  class UIWidgetEvent { public EventHandler Clicked; }   class NonVisualUIWidget {public EventHandler Expired;}  class TestSample {       public void Test()     {           new Sample().SupportedEvents.Clicked += ...         new Sample2().SupportedEvents.Expired += ...     }   }   

Then I didn’t like SupportedEvents I want to be able to say

new Sample().Clicked +=... 

Then I thought JavaScript (I know C# is not JS :))… AND IDispatchEx, IL Weaving, Reflection.Emit etc. etc. and thought may be there’s a way to do this… [Design time support would be nice but I can live without]

Yes, I probably could do this ‘instance augmentation’ with a Visitor pattern. [Not sure if I could get the syntatic sugar though]

Comments?

  • 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. 2026-05-11T00:55:05+00:00Added an answer on May 11, 2026 at 12:55 am

    Well, you could create a DynamicMethod instance for your ‘new’ methods, but statically attaching them to an existing instance at runtime wouldn’t work due to the fact it plain wouldn’t compile.

    You might (I haven’t tried this) be able to emit the opcodes into an in-memory assembly, but that’s about as far away from being ‘Syntactically sweet’ as you can get (would involve a lot of reflection and InvokeMember calls, I would think)

    It also might be worth looking into Extension Methods – although I’ve never tried attaching events or event-like methods via extension methods…and they are 3.5 only, so that may limit you.

    The nicest looking, ‘pure C#’ implementation is probably something very similar to what you’ve already got with the generic/interface setup…

    Honestly, if you’re looking for something with true ‘dynamic support’ like this, I’d do this kind of stuff in a DLR-capable language (like IronPython) and call into it from your C# stuff.

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

Sidebar

Ask A Question

Stats

  • Questions 191k
  • Answers 191k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer class A { public $property = 'Foobar'; } class B… May 12, 2026 at 6:07 pm
  • Editorial Team
    Editorial Team added an answer There's no way for the DLL to read them, but… May 12, 2026 at 6:07 pm
  • Editorial Team
    Editorial Team added an answer When you write code test-first, you write against the public… May 12, 2026 at 6:07 pm

Related Questions

In C#, if you have multiple constructors, you can do something like this: public
Title says it mostly. I want to add a simple extension method to the
Rails introduced some core extensions to Ruby like 3.days.from_now which returns, as you'd expect
I added a reference to the System.Core assembly. The web.config now has: <add assembly=System.Core,

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.