假设我有类似的课程
interface ISampleInterface { void SampleMethod(); } class ImplementationClass : ISampleInterface { // Explicit interface member implementation: void ISampleInterface.SampleMethod() { // Method implementation. } static void Main() { // Declare an interface instance. ISampleInterface obj = new ImplementationClass(); // Call the member. obj.SampleMethod(); } }
从main方法我怎样才能确定ImplementationClass类在编写如下代码之前实现ISampleInterface
SampleInterface obj = new ImplementationClass(); obj.SampleMethod();
有什么办法….请讨论.谢谢.
解决方法
你可以使用反射:
bool result = typeof(ISampleInterface).IsAssignableFrom(typeof(ImplementationClass));