explicit-interface

How do I use reflection to get properties explicitly implementing an interface?

More specifically, if I have: public class TempClass : TempInterface { int TempInterface.TempProperty { get; set; } int TempInterface.TempProperty2 { get; set; } public int TempProperty { get; set; } } public interface TempInterface { int TempProperty { ...

Why to Use Explicit Interface Implementation To Invoke a Protected Method?

When browsing ASP.NET MVC source code in codeplex, I found it is common to have a class explicitly implementing interface. The explicitly implemented method/property then invoke another "protected virtual" method/property with same name. For example, public class MvcHandler : IHttpHandler, IRequiresSessionState { protected virtua...

How costly is boxing when explicitly implementing an interface

The current guidlelines for explicit member implementation recommend: Using explicit members to approximate private interface implementations. If you need to implement an interface for only infrastructure reasons and you never expect developers to directly call methods on that interface from this type then implement the members explici...

LinkedList(T) add-method

The Add-method from the ICollection(T) interface has been explicitly implemented by the LinkedList(T)-class. This collection instead have AddFirst- and AddLast-methods (among others). The explicitly implemented method maps to the AddLast-method. This has some drawbacks and IMHO no benefit at all. The two major drawbacks are this: You c...

Can one reference a same-named implicit property in an explicit Interface implementation?

Say I have a type that implements a property with a string type: public class Record { public string Value { get; set; } } Then I have an interface that defines a property with the same name: public interface IIntValued { public int Value { get; set; } } I can use explicit interface as follows: public class Record : IInt...

C++/CLI: Implementing IList and IList<T> (explicit implementation of a default indexer)

I am trying to implement a C++/CLI class that implements both IList and IList<T>. Since they have overlapping names, I have to implement one of them explicitly, and the natural choice should be IList. The implicit implementation of the indexer is: using namespace System::Collections::Generic; generic<class InnerT> public ref class MyL...

Why is HashSet<T>.IsReadOnly explicit?

This var h = new HashSet<int>(); var r = h.IsReadOnly; does not compile. I have to do var r = ((ICollection<int>)h).IsReadOnly; why wasn't IsReadOnly implemented normally? (I'm not asking how, but why) ...

How can I call explicitly implemented interface method from PowerShell?

Code: add-type @" public interface IFoo { void Foo(); } public class Bar : IFoo { void IFoo.Foo() { } } "@ -Language Csharp $bar = New-Object Bar ($bar -as [IFoo]).Foo() # ERROR. Error: Method invocation failed because [Bar] doesn't contain a method named 'Foo'. ...

Using Explicit Interfaces to prevent accidental modification of properties in C#

I stumbled on a feature of C# method resolution that I didn't notice before. Namely, when I explicitly implement an interface that supports a setter, and the implicit interface only offers a protected set, the compiler sensibly defers to the protected set when I call it. So I get most of the convenience of auto-implemented properties, bu...

C# - Explicit Interfaces with inheritance?

Output: B->Hello! from Explicit. Shouldn't it be:? A->Hello! from Explicit. Why doesn't explicit cast (IHello)a call IHello.Hello() from class A? interface IHello { void Hello(); } class A : IHello { public virtual void Hello() { Console.WriteLine("A->Hello!"); } void IHello.Hello() { Co...

How do I determine which interface is referenced by an explicitly-implemented MethodInfo object?

I have a MethodInfo object that represents an explicitly-implemented interface method, as follows. MethodInfo GetMethod() { return typeof(List<>).GetMethod( "System.Collections.IEnumerable.GetEnumerator", BindingFlags.Instance | BindingFlags.NonPublic); } How do I query this MethodInfo object to obtain the interfac...

C# property not available in derived class

I'm not sure what's going on. I have the following base class: public class MyRow : IStringIndexable, System.Collections.IEnumerable, ICollection<KeyValuePair<string, string>>, IEnumerable<KeyValuePair<string, string>>, IDictionary<string, string> { ICollection<string> IDictionary<string, string>.Keys { } } And then I...

Explicitly implemented interface and generic constraint

interface IBar { void Hidden(); } class Foo : IBar { public void Visible() { /*...*/ } void IBar.Hidden() { /*...*/ } } class Program { static T CallHidden1<T>(T foo) where T : Foo { foo.Visible(); ((IBar)foo).Hidden(); //Cast required return foo; } static T CallHidden2<T>(T foo) where T : Fo...

Object initializer with explicit interface in C#

How can I use an object initializer with an explicit interface implementation in C#? public interface IType { string Property1 { get; set; } } public class Type1 : IType { string IType.Property1 { get; set; } } ... //doesn't work var v = new Type1 { IType.Property1 = "myString" }; ...

Stubbing out methods that explicitly implement an interface using Rhino Mocks

How can I stub out methods that explicitly implement an interface using Rhino Mocks? As I understand it, Rhino Mocks requires stubbed out methods to be virtual, and explicitly implemented interface members are not virtual. ...

Why would a class implement IDisposable explicitly instead of implicitly?

I was using the FtpWebResponse class and didn't see a Dispose method. It turns out that the class implements IDisposable, but does so explicitly so that you must first cast your instance to IDisposable before calling Dispose: // response is an instance of FtpWebResposne ((IDisposable) response).Dispose(); Why would the designer of a ...

How are explicit interface implementations implemented in IL?

I've been having a look at explicit interface implementations in IL. The method Method in the following class (interface IA has a single Method() on it): public class B : IA object IA.Method() { /* code */ } } is compiled to the following IL method signature: .method private hidebysig newslot virtual final instance ob...