explicit-interface

Type parameter 'T' has the same name as the type parameter from outer type '…'

杀马特。学长 韩版系。学妹 提交于 2019-12-09 02:20:12
问题 public abstract class EntityBase { ... } public interface IFoobar { void Foo<T>(int x) where T : EntityBase, new(); } public interface IFoobar<T> where T : EntityBase, new() { void Foo(int x); } public class Foobar<T> : IFoobar, IFoobar<T> where T : EntityBase, new() { public void Foo(int x) { ... } void IFoobar.Foo<T>(int x) { Foo(x); } } I get a compiler warning: Type parameter 'T' has the same name as the type parameter from outer type '...' I tried doing: void IFoobar.Foo<U>(int x) { Foo

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

跟風遠走 提交于 2019-12-06 09:54:13
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, but I can prevent accidental modification of fields by clients who shouldn't be changing them. As an example, virtual public DateTime CreatedOn { get; protected set; } virtual public DateTime? ModifiedOn { get; protected set; } #region IHaveUpdateDateFields

Explicitly implemented interface and generic constraint

て烟熏妆下的殇ゞ 提交于 2019-12-04 04:04:44
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 : Foo, IBar { foo.Visible(); foo.Hidden(); //OK return foo; } } Is there any difference (CallHidden1 vs. CallHidden2) is actual compiled code? Is there other differences between where T : Foo and where T : Foo, IBar (if Foo implements IBar) that in accessing explicitly implemented interface members ? The IL

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

橙三吉。 提交于 2019-12-03 20:36:05
问题 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 { get; set; } int TempProperty2 { get; set; } } How do I use reflection to get all the propertyInfos for properties explicitly implementing TempInterface? Thanks. 回答1: I think the class you are looking for is System.Reflection.InterfaceMapping.

Explicit implementation of IDisposable

我是研究僧i 提交于 2019-12-03 17:13:18
问题 Although there are quite a lot of Q&As regarding IDisposable to be found on SO, I haven't found an answer to this yet: I usually follow the practice that when one of my classes owns an IDisposable object then it also implements IDisposable and calls Dispose on the owned object. However recently I came across a class which implemented IDisposable explicitly thus preventing me from directly calling Dispose forcing me to cast it which I found annoying and unnecessary. So the question: Why and

Explicit interface implementation cannot be virtual

风流意气都作罢 提交于 2019-12-03 11:55:33
问题 For the record, I've already seen this connect item but I can't really understand what would be the problem in supporting this. Say I have the following code: public interface IInterface { void Method(); } public class Base : IInterface { virtual void IInterface.Method() { throw new NotImplementedException(); } } what is the problem with the virtual identifier? Having a virtual modifier would make it possible to override indicating there's a different implementation in the base class. I can

Explicit implementation of IDisposable

人走茶凉 提交于 2019-12-03 05:26:32
Although there are quite a lot of Q&As regarding IDisposable to be found on SO, I haven't found an answer to this yet: I usually follow the practice that when one of my classes owns an IDisposable object then it also implements IDisposable and calls Dispose on the owned object. However recently I came across a class which implemented IDisposable explicitly thus preventing me from directly calling Dispose forcing me to cast it which I found annoying and unnecessary. So the question: Why and when would one want to use an explicit interface implementation of IDisposable ? I know that there are

Explicit interface implementation cannot be virtual

北战南征 提交于 2019-12-03 02:25:31
For the record, I've already seen this connect item but I can't really understand what would be the problem in supporting this. Say I have the following code: public interface IInterface { void Method(); } public class Base : IInterface { virtual void IInterface.Method() { throw new NotImplementedException(); } } what is the problem with the virtual identifier? Having a virtual modifier would make it possible to override indicating there's a different implementation in the base class. I can make it work now by removing the virtual method and creating the derived class like this: public class

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

混江龙づ霸主 提交于 2019-11-30 17:06:13
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 { get; set; } int TempProperty2 { get; set; } } How do I use reflection to get all the propertyInfos for properties explicitly implementing TempInterface? Thanks. I think the class you are looking for is System.Reflection.InterfaceMapping. Type ifaceType = typeof(TempInterface); Type tempType = typeof(TempClass); InterfaceMapping map = tempType

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

牧云@^-^@ 提交于 2019-11-30 15:03:38
问题 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 virtual bool IsReusable { get { return false; } } bool IHttpHandler.IsReusable { get { return IsReusable; } } } I'm now sure what's the benefit of this kind of programming.