Andy Smith's Blog

Page.RegisterStartupScript('Andy', 'MetaBuilders_WebControls_GainKnowledge();');

How to extend an explicitly implemented interface

Let's say you were creating a control that derives from a built in webcontrol. You might want to add some new fun stuff to the current control's implementation of IPostBackDataHandler. Unfortunately, all the built in controls implement IPostBackDataHandler privately, using c#'s explicit interface implementation feature. What this means to you is that there is no built-into-the-language way to override that implementation at all. If you implement the interface, only your implementation can ever be called. So if you want to extend an explicitly implemented interface, you actually have to reimplement everything that the base class does in its implementation. Ack.

As you can imagine, I was not happy with that. So, I came up with a hack way around this problem that serves my needs.

public interface IFoo {
  void Bar();
}
public class ClassA : IFoo {
  void IFoo.Bar() {
    Console.WriteLine("IFoo.Bar from ClassA");
  }
}
public class ClassB : ClassA, IFoo {
  void IFoo.Bar() {
    base_IFoo_Bar();
    Console.WriteLine("IFoo.Bar from ClassB");
  }
  private void base_IFoo_Bar() {
    //Here's the fun stuff!
    System.Reflection.InterfaceMapping interfaceMap = typeof(ClassA).GetInterfaceMap(typeof(IFoo));
    for ( Int32 i=0; i< interfaceMap.InterfaceMethods.Length; i++ ) {
      if ( interfaceMap.InterfaceMethods[i].Name == "Bar" ) {
        interfaceMap.TargetMethods[i].Invoke(this,null);
        break;
      }
    }
  }
}

Comments

chloe bags said:

buxu yao zheyang ma

# August 2, 2011 4:26 AM

Wholesale Caps said:

Interesting posts here.. gracias for sharing so much in your blog.. Greets.

# August 16, 2011 4:46 AM

Louis Vuitton Clothing said:

That is an awfully astounding column you’ve posted.Thanks a lot for that a fantastically amazing post!

# September 2, 2011 4:19 AM

Air Max store said:

Its a great start of the day with a website like this. very informative , im now one of the regular visitor of your web. Thanks.

# September 3, 2011 6:21 AM

belstaff uk said:

Iwas really attracted by such a good product , I think only a good product to be able to get customer support, only honest companies can be sustainable development.

# September 27, 2011 10:22 PM

abercrombie and fitch deutschland said:

abercrombie and fitch deutschland

# October 10, 2011 11:23 AM

Louis Vuitton Outlet said:

Thanks for posting this useful information.

# October 31, 2011 4:30 AM

Cheap Hats said:

I love his company most of the time, but he is very dark in his opinions. Ironically, he is very giving.

# November 2, 2011 3:17 AM

moncler coat said:

It is easy for you to find a variety of Cheap Moncler jacket with high quality here. Cheap Moncler jacket are all well designed,with excellent manufacture and the prices of which is attractive.When it comes to the Moncler down jacket,what we know is that it is famous in the world.It is made of top materials and is really warmness in cold weather.

# November 10, 2011 7:47 PM

xatral ureteric stones said:

Spit on the Stane, and it will be wet at the last.

# November 23, 2011 6:27 PM

Blog Writers said:

I read above post it is very informative

# April 17, 2012 3:27 AM

Sandpoint Idaho real estate said:

Inland Northwest Realty, Sandpoint, Idaho real estate listings, homes for sale.  Your real estate Sandpoint, Idaho resource center, find mls listings, condos and homes for sale in Sandpoint Idaho.

# April 23, 2012 10:06 AM

oem software said:

PolF7V A round of applause for your blog article.Thanks Again. Fantastic.

# May 2, 2012 5:10 AM

http://www.wow-gold-team.com said:

As a Newbie, I am continuously searching online for articles that can help me. Thank you

# October 12, 2012 5:59 PM

tmxdimycwqf@yahoo.ca said:

Nice Articles. My thanks for doing such a nice report. I will return here to find out more and recommend my neighbors about your articles.

# April 20, 2013 7:23 AM

Gordon said:

The importance of very good health care can be seen in the hopes of a individuals who are yearning for health.

For being rich or to produce something in your life, you have

to have that capability or power. If you are ill, it is possible not to develop your self by any means.

Therefore overall health is wealth and this is the biggest lesson that people can learn currently.

# April 20, 2013 9:42 AM