附测试代码。
要求:不修改接口和两个类的内容,实现在Main中对MyClass1的实例调用MyClass中的a方法。反射也可
using System;
namespace ConsoleApplication1
{
    interface IInterface
    {
        void a();
    }
    class MyClass : IInterface
    {
        void IInterface.a()
        {
            Console.WriteLine("base class");    //对于一个MyClass1实例,如何调用这个方法??
        }
    }
    class MyClass1 : MyClass, IInterface
    {
        void IInterface.a()
        {
            Console.WriteLine("inherit class");
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            IInterface i = new MyClass1();
            i.a();
        }
    }
}

解决方案 »

  1.   

    不是有个base类吗?调用这个试试。
      

  2.   

    这种实现太诡异。不明白为什么实现的时候需要用void IInterface.a(),否则还是很好实现。
      

  3.   

    本身设计就有问题.干嘛要显示继承接口??? //看看下面设计是否符合你的要求using System;
    namespace ConsoleApplication1
    {
        interface IInterface
        {
           void a();
        }
        class MyClass : IInterface
        {
          public void a()
            {
                Console.WriteLine("base class");    //对于一个MyClass1实例,如何调用这个方法??
            }    }
        class MyClass1 : MyClass, IInterface
        {
            virtual public void a()
            {
                Console.WriteLine("inherit class");
            }
        }
        class Program
        {
            static void Main(string[] args)
            {
                MyClass i = new MyClass1();
                i.a();            IInterface i2 = new MyClass1();
                i2.a();        }
        }
    }
      

  4.   

    显示实现接口的方法,那么该方法不能作为类的实例的方法来调用,只能在类的内部使用,这也就是为什么显示实现的方法不用加“public”等成员限定符的原因,因为加不加没有任何意义。
    http://www.winu.cn/space-14160-do-blog-id-24202.html
      

  5.   

    很不幸,这个解释是错的,而且错的离谱...你所加深的是错误认识...接口成员都必须是public成员,怎么可能“只能在类的内部使用”?至于不需要加访问修饰符,是因为显式实现的都是直接属于接口的成员...下面才是显式实现接口的正确用法...
    interface IInterface
    {
        void a();
    }
    class MyClass : IInterface
    {
        void IInterface.a()
        {
            Console.WriteLine("base class");
        }
    }
    class MyClass1 : MyClass
    {
        public void a()
        {
            Console.WriteLine("inherit class");
        }
    }MyClass1 myClass = new MyClass1();
    myClass.a();IInterface myInterface = new MyClass1();
    myInterface.a();
      

  6.   

    改成这样更好理解...
    interface IInterface
    {
        void a();
    }
    class MyClass : IInterface
    {
        public void a()
        {
            Console.WriteLine("base class");
        }
    }
    class MyClass1 : MyClass,IInterface
    {
        void IInterface.a()
        {
            Console.WriteLine("inherit class");
        }
    }
      

  7.   

    回复:vrhero
    若批评不自由则赞美无意义
    ---------------------
    我想,你没明白我的意思,更没明白楼主的意思。
    现在讨论的是他的这种情况,而不是在优化代码,显然你这样写很合理,但楼主的问题是想搞明白显式实现接口的意义。
    我发的帖子都是测试过的,所以我也习惯性测试了你的例子,也是正确的。
    但是,我们要从楼主的问题出发,毕竟需求才是开发的终点。IInterface i = new MyClass1();
    i.a();看到他2句了么,i是什么,是接口IInterface的引用,指向MyClass1的对象,那我尝试这样做,没错吧
    MyClass1 i = new MyClass1();
    i.a();
    效果应该一样吧,但编译不通过。class MyClass : IInterface
    {
        public void IInterface.a()
        {
            Console.WriteLine("base class");    //对于一个MyClass1实例,如何调用这个方法??
        }
    }
    于是这样修改,发现无法编译通过。所以我搜索得到那个结论,那么你说那个博客写的不对,请指正一下错误原因吧,洗耳恭听 ^_^
      

  8.   

    一会到msdn上看看,先猜测一下,显式实现的接口函数就是只能通过接口引用调用的不能作为虚函数的特殊的私有函数。对么?
      

  9.   

    但的确
    class MyClass : IInterface
    {
        public void IInterface.a()
        {
            Console.WriteLine("base class");    //对于一个MyClass1实例,如何调用这个方法??
        }
    }无法编译通过哦。
      

  10.   

    另外,楼主的问题显然不想搞明白显式接口实现的意义...否则就不会有“不修改接口和两个类的内容”的要求...所以我懒得回答...如果你想真正搞清楚显式接口实现的意义,MSDN写得很明确...
      

  11.   

    多谢vrhero耐心讲解。受教了。我还找英文版说明呢,原来中文的msdn里已经很详细了。意思了解了,但这样看来,国内多数的blog上文章的措辞都不准确。用法描述的是对的,但似乎曲解了微软的意图。
      

  12.   

    从解决问题的角度来看9楼的代码正解。不过wuyazhe同志与vrhero同志的讨论可以让对显式接口不是很熟悉的同志们趁机了解一下说的很不错。
      

  13.   


    static void Main(string[] args)
            {
                IInterface i = new MyClass1();
                i.a();            i.GetType().GetInterface("IInterface").GetMethod("a").Invoke(Activator.CreateInstance(iif.GetType
    ().BaseType), null);
            }ps: 虽然可以实现,但这不是一个好的方案