How to access the explicit interface implementation of a base class from a dervied class ?
-
Is there a way to access the explicit interface implementation of a base class from a dervied class without using reflection ? Because the following lines wont compile, and i dont understand why i shouldn't.
public interface ISomeInterface {
void SomeMethode();
}
public class BaseClass : ISomeInterface {
object SomeInterface.SomeMethode{ ... }
}
public class DerivedClass : BaseClass, ISomeInterface {
object SomeInterface.SomeMethode{ ( ( ISomeInterface ) base ).SomeMethode(); // CS0175 - Use of keyword base is not valid in this context }
}
-
Is there a way to access the explicit interface implementation of a base class from a dervied class without using reflection ? Because the following lines wont compile, and i dont understand why i shouldn't.
public interface ISomeInterface {
void SomeMethode();
}
public class BaseClass : ISomeInterface {
object SomeInterface.SomeMethode{ ... }
}
public class DerivedClass : BaseClass, ISomeInterface {
object SomeInterface.SomeMethode{ ( ( ISomeInterface ) base ).SomeMethode(); // CS0175 - Use of keyword base is not valid in this context }
}
Why do you declare
public class DerivedClass : BaseClass, ISomeInterface
at all? Shouldn'tpublic class DerivedClass : BaseClass
be enough to ensure that DerivedClass implements ISomeInterface? -
Why do you declare
public class DerivedClass : BaseClass, ISomeInterface
at all? Shouldn'tpublic class DerivedClass : BaseClass
be enough to ensure that DerivedClass implements ISomeInterface? -
-
Is there a way to access the explicit interface implementation of a base class from a dervied class without using reflection ? Because the following lines wont compile, and i dont understand why i shouldn't.
public interface ISomeInterface {
void SomeMethode();
}
public class BaseClass : ISomeInterface {
object SomeInterface.SomeMethode{ ... }
}
public class DerivedClass : BaseClass, ISomeInterface {
object SomeInterface.SomeMethode{ ( ( ISomeInterface ) base ).SomeMethode(); // CS0175 - Use of keyword base is not valid in this context }
}
As ive said above its not entirely clear what you are trying to achieve. However, you can access that explicit interface in the derived class without also implementing the interface.
public interface ISomeInterface{
void SomeMethod();
}public class BaseClass : ISomeInterface {
void SomeInterface.SomeMethode(){
...
}
}public class DerivedClass : BaseClass{
void AnotherMethod(){
//This wont work, its implemented explicitly
//this.SomeMethod()
//This will work
((ISomeInterface)this).SomeMethod();
}
}Incidentally if you change BaseClass too the following the commented out line above will work. This is implementing the interface implicitly
public class BaseClass : ISomeInterface {
public void SomeMethode(){
...
}
} -
Is there a way to access the explicit interface implementation of a base class from a dervied class without using reflection ? Because the following lines wont compile, and i dont understand why i shouldn't.
public interface ISomeInterface {
void SomeMethode();
}
public class BaseClass : ISomeInterface {
object SomeInterface.SomeMethode{ ... }
}
public class DerivedClass : BaseClass, ISomeInterface {
object SomeInterface.SomeMethode{ ( ( ISomeInterface ) base ).SomeMethode(); // CS0175 - Use of keyword base is not valid in this context }
}
J4amieC has given you the correct answer above. It's the difference between explicit and implicit. If you want to change a value from one type to another in code then if an implicit operator exists you can just set one to equal the other. If not, you have to explicitly cast to the type you require. The same applies to interfaces. If you implement them explicitly then the cast is required in any classes that derive from the class where the implementation was made.
((ISomeInterface)this).SomeMethode();
Dave
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
Expect everything to be hard and then enjoy the things that come easy. (code-frog)