explicit-implementation

Use explicit interface implementations with a dynamic object

北战南征 提交于 2020-01-20 06:52:02
问题 I'm experimenting with explicit implentations of interfaces. This is to strip the intellisense with methods which are not valid in the current context. Use /practical-applications-of-the-adaptive-interface-pattern-the-fluent-builder-context/ as reference. To prove that they would not be callable, I thought I could use the dynamic keyword, because then at least my code would compile. It does compile, but it does not work as expected. The dynamic variable has access to the class methods, but

Use explicit interface implementations with a dynamic object

孤人 提交于 2020-01-20 06:49:09
问题 I'm experimenting with explicit implentations of interfaces. This is to strip the intellisense with methods which are not valid in the current context. Use /practical-applications-of-the-adaptive-interface-pattern-the-fluent-builder-context/ as reference. To prove that they would not be callable, I thought I could use the dynamic keyword, because then at least my code would compile. It does compile, but it does not work as expected. The dynamic variable has access to the class methods, but

Explicit C# interface implementation of interfaces that inherit from other interfaces

青春壹個敷衍的年華 提交于 2019-12-23 12:45:36
问题 Consider the following three interfaces: interface IBaseInterface { event EventHandler SomeEvent; } interface IInterface1 : IBaseInterface { ... } interface IInterface2 : IBaseInterface { ... } Now consider the following class that implements both IInterface1 and IInterface 2: class Foo : IInterface1, IInterface2 { event EventHandler IInterface1.SomeEvent { add { ... } remove { ... } } event EventHandler IInterface2.SomeEvent { add { ... } remove { ... } } } This results in an error because

How to call an explicitly implemented interface-method on the base class

£可爱£侵袭症+ 提交于 2019-12-17 18:16:12
问题 I have a situation, where two classes (one deriving from the other) both implement the same interface explicitly: interface I { int M(); } class A : I { int I.M() { return 1; } } class B : A, I { int I.M() { return 2; } } From the derived class' implementation of I.M() , I'd like to call the implementation of the base class, but I don't see how to do it. What I tried so far is this (in class B): int I.M() { return (base as I).M() + 2; } // this gives a compile-time error //error CS0175: Use

How can I access an explicitly implemented method using reflection?

与世无争的帅哥 提交于 2019-12-04 12:19:45
问题 Usually, I access a method in reflection like this: class Foo { public void M () { var m = this.GetType ().GetMethod ("M"); m.Invoke(this, new object[] {}); // notice the pun } } However, this fails when M is an explicit implementation: class Foo : SomeBase { void SomeBase.M () { var m = this.GetType ().GetMethod ("M"); m.Invoke(this, new object[] {}); // fails as m is null } } How do I access an explicitly implemented method using reflection? 回答1: It's because the name of the method is not

Is the C# “explicit implementation” of the interface present in Java?

こ雲淡風輕ζ 提交于 2019-11-30 07:55:02
问题 In C#, if you have two base interfaces with the same method (say, F()) you can use explicit implementation to perform different impl. for F(). This alloes you to differently treat the object, corresponding to the current point of view: as IMyInterface1 or IMyInterface2. Is this possible in Java? 回答1: No, there's nothing like C#'s explicit interface implementation in Java. On the plus side, Java has covariant return types, so if you want to provide a more strongly typed implementation than the

C#: Property overriding by specifying the interface explicitly

泄露秘密 提交于 2019-11-29 11:41:20
问题 While attempting to override the explicit interface implementation of the ICollection<T>.IsReadOnly property from the Collection<T> class, I came across some documents stating that explicit interface member implementations cannot be overridden because they cannot have modifiers such as virtual or abstract . On MSDN they even go as far as specifying how to make an explicit interface member implementation available for inheritance by creating another abstract or virtual member which is called

Is the C# “explicit implementation” of the interface present in Java?

那年仲夏 提交于 2019-11-29 05:38:30
In C#, if you have two base interfaces with the same method (say, F()) you can use explicit implementation to perform different impl. for F(). This alloes you to differently treat the object, corresponding to the current point of view: as IMyInterface1 or IMyInterface2. Is this possible in Java? No, there's nothing like C#'s explicit interface implementation in Java. On the plus side, Java has covariant return types, so if you want to provide a more strongly typed implementation than the interface specifies, that's okay. For instance, this is fine: interface Foo { Object getBar(); } public

How to call an explicitly implemented interface-method on the base class

吃可爱长大的小学妹 提交于 2019-11-28 06:47:32
I have a situation, where two classes (one deriving from the other) both implement the same interface explicitly: interface I { int M(); } class A : I { int I.M() { return 1; } } class B : A, I { int I.M() { return 2; } } From the derived class' implementation of I.M() , I'd like to call the implementation of the base class, but I don't see how to do it. What I tried so far is this (in class B): int I.M() { return (base as I).M() + 2; } // this gives a compile-time error //error CS0175: Use of keyword 'base' is not valid in this context int I.M() { return ((this as A) as I).M() + 2; } // this

C# Language Design: explicit interface implementation of an event

℡╲_俬逩灬. 提交于 2019-11-27 14:22:40
问题 Small question about C# language design :)) If I had an interface like this: interface IFoo { int Value { get; set; } } It's possible to explicitly implement such interface using C# 3.0 auto-implemented properties: sealed class Foo : IFoo { int IFoo.Value { get; set; } } But if I had an event in the interface: interface IFoo { event EventHandler Event; } And trying to explicitly implement it using field-like event: sealed class Foo : IFoo { event EventHandler IFoo.Event; } I will get the