将接口从vb.net转换为c#

Jul*_*les 1 .net c# vb.net interface

我有一个控件覆盖受保护的GetService方法并将其分配给IServiceProvider接口:

Class MyControl
    Inherits Control
    Implements IServiceProvider

    Protected Overrides Sub GetService(t as Type) as Object Implements IServiceProvider.GetService
    End Sub

End Class
Run Code Online (Sandbox Code Playgroud)

我正在努力将其转换为c#.我试过隐式v.明确但我必须得到错误的语法.

Ree*_*sey 7

你会这样做:

class MyControl : Control, IServiceProvider
{
     // Explicitly implement this
     object IServiceProvider.GetService(Type t)
     {
          // Call through to the protected version
          return this.GetService(t);
     }

     // Override the protected version...
     protected override object GetService(Type t)
     {
     }
}
Run Code Online (Sandbox Code Playgroud)

话虽这么说,Control已经实现了IServiceProvider(通过Component).你真的可以这样做:

class MyControl : Control
{
     protected override object GetService(Type t)
     {
     }
}
Run Code Online (Sandbox Code Playgroud)

  • 这不起作用.它说该方法无法实现,因为它不公开. (2认同)