标签: explicit-interface

显式接口实现不能是虚拟的

为了记录,我已经看过这个连接项,但我真的不明白支持这个问题会是什么.

说我有以下代码:

public interface IInterface
{
    void Method();
}

public class Base : IInterface
{
    virtual void IInterface.Method()
    {
        throw new NotImplementedException();
    }
}
Run Code Online (Sandbox Code Playgroud)

虚拟标识符有什么问题?使用虚拟修饰符可以override指示基类中有不同的实现.我现在可以通过删除虚方法并像这样创建派生类来使其工作:

public class Derived : IInterface
{
    void IInterface.Method()
    {
        throw new NotImplementedException();
    }
}
Run Code Online (Sandbox Code Playgroud)

但是这样我真的没有迹象表明我压倒了什么.

更新:
根据C#(部分:20.4.1显式接口成员实现)规范,有两个原因.

  1. 隐藏某些方法(我正在使用它).
  2. 具有相同签名但返回类型不同的2个函数(例如,对IClonable有用).

它没有说明为什么你不能使这些方法虚拟.

更新2:
鉴于答案,我认为我应该在这里重新提出真正的问题.如果以上两个原因是首先使接口的显式实现成为可能的原因.如果将方法设为虚拟,为什么会出现问题?

c# compiler-construction interface explicit-interface

13
推荐指数
2
解决办法
5273
查看次数

在F#中调用基类显式接口方法

好的,我B从基类派生出一个类型A. A实现IDisposable显式但我必须进行额外的清理B,所以我实现IDisposableB:

interface IDisposable with
    member i.Dispose() =
        // ... additional work
        base.Dispose() // <- want to do but cannot
Run Code Online (Sandbox Code Playgroud)

问题是:如何从base访问Dispose-method?

(base :> IDisposable).Dispose()
Run Code Online (Sandbox Code Playgroud)

产生编译错误: Unexpected symbol ':>' in expression. Expected '.' or other token.

做点什么

(i :> IDisposable).Dispose()
Run Code Online (Sandbox Code Playgroud)

当然会产生一个StackOverflowException运行时 - 所以我该怎么做呢?对不起,但从未遇到过这样的事情...

inheritance f# explicit-interface

13
推荐指数
2
解决办法
1949
查看次数

为什么HashSet <T> .IsReadOnly显式?

这个

var h = new HashSet<int>();
var r = h.IsReadOnly;
Run Code Online (Sandbox Code Playgroud)

不编译.我要做

var r = ((ICollection<int>)h).IsReadOnly;
Run Code Online (Sandbox Code Playgroud)

为什么IsReadOnly没有正常实施?

(我不是问怎么样,但为什么)

c# interface hashset explicit-interface icollection

12
推荐指数
2
解决办法
3486
查看次数

为什么使用显式接口实现来调用受保护的方法?

codeplex中浏览ASP.NET MVC源代码时,我发现有一个类显式实现接口是很常见的.显式实现的方法/属性然后调用具有相同名称的另一个"受保护的虚拟"方法/属性.

例如,

public class MvcHandler : IHttpHandler, IRequiresSessionState 
{
    protected virtual bool IsReusable 
    {
        get 
        {
           return false;
        }
    }

    bool IHttpHandler.IsReusable 
    {
        get 
        {
           return IsReusable;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我现在确定这种编程的好处是什么.对我来说,我更喜欢隐式实现接口IHttpHandler.

我猜作者只是不希望MvcHandler有一个公共属性IsResuable.仅当MvcHandler的实例被视为IHttpHandler时,才能使用属性IsReusable.不过,我不确定为什么作者这样做.

有谁知道这种界面实现的更多好处?

c# interface implicit explicit-interface

10
推荐指数
1
解决办法
2689
查看次数

C#中具有显式接口的对象初始值设定项

如何在C#中使用具有显式接口实现的对象初始化程序?

public interface IType
{
  string Property1 { get; set; }
}

public class Type1 : IType
{
  string IType.Property1 { get; set; }
}

...

//doesn't work
var v = new Type1 { IType.Property1 = "myString" };
Run Code Online (Sandbox Code Playgroud)

c# explicit-interface

10
推荐指数
1
解决办法
4414
查看次数

C# - 具有继承的显式接口?

输出:
B->你好!来自明确.

不应该是:?
A->您好!来自明确.

为什么没有显式演员(IHello)从A班调用IHello.Hello()?

interface IHello
{
    void Hello();
}

class A : IHello
{

    public virtual void Hello()
    {
        Console.WriteLine("A->Hello!");
    }

    void IHello.Hello()
    {
        Console.WriteLine("A->Hello! from Explicit.");
    }
}

class B : A, IHello
{
    public override void Hello()
    {
        Console.WriteLine("B->Hello!");
    }

    void IHello.Hello()
    {
        Console.WriteLine("B->Hello! from Explicit.");
    }
}

class Program
{
    static void Main(string[] args)
    {
        A a = new B();
        ((IHello)a).Hello();
    }
}
Run Code Online (Sandbox Code Playgroud)

c# inheritance explicit-interface

9
推荐指数
1
解决办法
2869
查看次数

Fortran - 显式接口

我对Fortran很新,而且对于我的研究,我需要让一个模型运行的怪物,所以我正在学习,因为我正在进行.如果我问一个"愚蠢"的问题,我很抱歉.我正在尝试编译(Mac OSX,从命令行),我已经设法解决了一些问题,但现在我遇到了一些我不确定如何解决的问题.我想我得到了错误背后的想法,但同样,不知道如何解决.

模型很大,所以我只会发布我认为相关的代码部分(虽然我可能是错的).我有一个包含几个子程序的文件,它以:

    !==========================================================================================!
!    This subroutine simply updates the budget variables.                                  !
!------------------------------------------------------------------------------------------!
subroutine update_budget(csite,lsl,ipaa,ipaz)

   use ed_state_vars, only : sitetype     ! ! structure
   implicit none

   !----- Arguments -----------------------------------------------------------------------!
   type(sitetype)  , target     :: csite
   integer         , intent(in) :: lsl
   integer         , intent(in) :: ipaa
   integer         , intent(in) :: ipaz
   !----- Local variables. ----------------------------------------------------------------!
   integer                      :: ipa
   !----- External functions. -------------------------------------------------------------!
   real            , external   :: compute_water_storage
   real            , external   :: compute_energy_storage
   real            , external   :: compute_co2_storage
   !---------------------------------------------------------------------------------------!


   do ipa=ipaa,ipaz
      !------------------------------------------------------------------------------------!
      ! …
Run Code Online (Sandbox Code Playgroud)

fortran compilation explicit-interface

9
推荐指数
1
解决办法
1万
查看次数

依赖注入和显式接口实现

在依赖注入方面有明确的实现接口的好处吗?

据我所知,接口可以显式或隐式实现:

interface IFoo
{
    void Bar();
}

//implicit implementation
class Foo1 : IFoo
{
    public void Bar(){}
}

//explicit implementation
class Foo2 : IFoo
{
    void IFoo.Bar(){}
}
Run Code Online (Sandbox Code Playgroud)

现在只能通过调用接口方法来调用显式实现,而可以直接在类的实例上调用隐式实现:

class Baz
{
    void Ba()
    {
        Foo1 foo1 = new Foo1();
        foo1.Bar();

        Foo2 foo2 = new Foo2();
        foo2.Bar();    //syntax error

        IFoo foo2_explicit = new Foo2();
        foo2_explicit.Bar();
    }
}
Run Code Online (Sandbox Code Playgroud)

因此,使用显式接口实现,不能意外地在具体类上调用方法,但必须调用接口方法.这是否会阻止紧密耦合的代码,这是DI的一个目的,还是我在这里咆哮错误的树?毕竟,不能意外地编写一个构造函数或方法来获取注入的具体类而不是接口:

class Baz
{
    void Ba(Foo2 foo)
    {
        foo.Bar(); //syntax error
    }

    void Bb(IFoo foo)
    {
        foo.Bar();
    }
}
Run Code Online (Sandbox Code Playgroud)

c# dependency-injection explicit-interface

9
推荐指数
1
解决办法
2321
查看次数

明确实现的接口和泛型约束

interface IBar { void Hidden(); }

class Foo : IBar { public void Visible() { /*...*/ } void IBar.Hidden() { /*...*/ } }

class Program
{
    static T CallHidden1<T>(T foo) where T : Foo
    {
        foo.Visible();
        ((IBar)foo).Hidden();   //Cast required

        return foo;
    }

    static T CallHidden2<T>(T foo) where T : Foo, IBar
    {
        foo.Visible();
        foo.Hidden();   //OK

        return foo;
    }
}
Run Code Online (Sandbox Code Playgroud)

是否有任何区别(CallHidden1与CallHidden2)是实际编译的代码?T:Foo和T:Foo,IBar(如果Foo实现IBar)在访问显式实现的接口成员时是否存在其他差异?

c# generics explicit-interface

7
推荐指数
2
解决办法
803
查看次数

为什么VS Metadata视图不显示显式接口实现的成员

前几天我正在查看C#Boolean struct元数据.

Boolean实现了IConvertible接口.但看看布尔的成员,我看不到大多数IConvertible成员.

我和一些同事做了一些测试,包括创建我们自己的类,并得出结论,IConvertible必须明确地为Boolean实现.

问题是,为什么它们不可见?我理解它可能是一个"按设计决定"但我明白如果检查元数据的任何人都可以看到它会增加更多的价值.

测试在VS2010 .NET4.0中完成

c# metadata explicit-interface

7
推荐指数
1
解决办法
777
查看次数