C# - Can't find usage of method when inherited and used through an interface implemented by the subclass

妖精的绣舞 提交于 2019-12-21 12:54:36

问题


I'm using VisualStudio 2008 and 2010, both with ReSharper, and when I try to lookup for usages of a given method I get no results, despite the method being inherited and then called through an interface. What's wrong? Is that a VS/ReSharper bug? See the example below:

using System;
namespace UsageNotFound
{
   interface MyInterface
   {
       void Hello();
   }

   class SuperClass
   {
       public void Hello() //NOTE: VS 2008/2010 (with resharper)
seems unable to find usages on this!!!
       {
           Console.WriteLine("Hi!");
       }
   }

   class SubClass : SuperClass, MyInterface
   {
       public static MyInterface GetInstance()
       {
           return new SubClass();
       }
   }

   class Program
   {
       static void Main(string[] args)
       {
           SubClass.GetInstance().Hello();
       }
   }
}

Thanks, Fabrizio


回答1:


This is a known issue: http://youtrack.jetbrains.net/issue/RSRP-46273 with no current target fix version




回答2:


Probably because your SuperClass doesn't implement the interface. This may cause a problem for ReSharper. Try:

   class SuperClass : MyInterface
   {
       public void Hello()
       {
           Console.WriteLine("Hi!");
       }
   }



回答3:


It's all to do with the order in which SubClass inherits the Hello() method from SuperClass and MyInterface.

I'm surprised that you don't get a warning that the SubClass.Hello() (from implementing the interface definition) will hide the SuperClass.Hello().



来源:https://stackoverflow.com/questions/7954318/c-sharp-cant-find-usage-of-method-when-inherited-and-used-through-an-interfac

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!