Click here to Skip to main content
Click here to Skip to main content

Assumed mistake in Explicit Interface implementation

, 28 Sep 2011
Rate this:
Please Sign up or sign in to vote.
Dear Friends, Again, i wont be discussing anything about Explicit interface implementation feature in c# here, i am sure either you know about it or there are many great articles on web you can take a look at. But i am must say that you must read the excellent article by my friend Abhiseksur on [...

Dear Friends,

Again, i wont be discussing anything about Explicit interface implementation feature in c# here, i am sure either you know about it or there are many great articles on web you can take a look at.

But i am must say that you must read the excellent article by my friend Abhiseksur on internals of Explicit interface implementation feature in C#

Let me outline particular point from Abhisek’s article which i or this article is interested in:

  • Explicit method is always implicitly private to its type
  • You can’t modify/specify any modifier to Explicit method.

So that means we cant do this as shown below:

1.   interface IMyInterface
2.    {
3.        void InterfaceMethod();
4.    }
5.
6.    class MyClass : IMyInterface
7.    {
8.        void IMyInterface.InterfaceMethod()
9.        {
10.
11.        }
12.    }
13.
       //Compiler Error, since InterfaceMethod is private to MyClass.
14.    new MyClass().InterfaceMethod()

So now any developer can be relaxed about this explicit implementation method that his code is fail proof since its private and no body can corrupt it. Yes logically at least it looks like that. But my curiousness doesn’t end here. Now lets see how safe is this Explicit implementation method in this class.

Assume the below code:

1.   abstract class MyAbstractClass
2.    {
3.        public int MyValue { get; set; }
4.    }
5.
6.    interface IMyInterface
7.    {
8.        void InterfaceMethod(MyAbstractClass absClassObj);
9.    }
10.
11.    class MyClass : IMyInterface
12.    {
13.        void IMyInterface.InterfaceMethod(MyAbstractClass absClassObj)
14.        {
15.            var value = absClassObj.MyValue;
16.        }
17.
18.        public void SomeOtherMethod()
19.        {
20.
21.        }
22.    }
23.
24.    class SomeEvilClass
25.    {
26.        MyClass myclass = new MyClass();
27.
28.        public void SomeEvilMethod()
29.        {
30.            (myclass as IMyInterface).InterfaceMethod(null);
31.        }
32.    }

This above code crashes the application. Although it’s not a great catch what i am speculating here, but could be a bit of flaw if the developer fails to over see it. I have no idea where and when this feature we make use off. But the workaround I am thinking off is to keep interface internal, at least you are sure that your assembly code is not evil and you can control it some how but on flip side it wont pay off much if i have interface which is not public.

That’s all friends, please leave comments for my learning’s :)

Thanks
Zen

License

This article, along with any associated source code and files, is licensed under The GNU Lesser General Public License (LGPLv3)

About the Author

zenwalker1985
Software Developer (Senior) Siemens
India India
A .net developer since 4+ years, wild, curious and adventurous nerd.
 
Loves Trekking/Hiking, animals and nature.
 
A FOSS/Linux maniac by default Wink | ;)
 
An MVP aspirant and loves blogging -> https://adventurouszen.wordpress.com/
Follow on   Twitter

Comments and Discussions

 
QuestionConfused PinmentorDaveyM6928-Sep-11 7:05 
AnswerRe: Confused Pinmemberzenwalker198528-Sep-11 8:27 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web03 | 2.8.140709.1 | Last Updated 28 Sep 2011
Article Copyright 2011 by zenwalker1985
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid