为了记录,我已经看过这个连接项,但我真的不明白支持这个问题会是什么.
说我有以下代码:
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显式接口成员实现)规范,有两个原因.
它没有说明为什么你不能使这些方法虚拟.
更新2:
鉴于答案,我认为我应该在这里重新提出真正的问题.如果以上两个原因是首先使接口的显式实现成为可能的原因.如果将方法设为虚拟,为什么会出现问题?
好的,我B从基类派生出一个类型A.
A实现IDisposable显式但我必须进行额外的清理B,所以我实现IDisposable在B:
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运行时 - 所以我该怎么做呢?对不起,但从未遇到过这样的事情...
这个
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没有正常实施?
(我不是问怎么样,但为什么)
在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#中使用具有显式接口实现的对象初始化程序?
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) 输出:
B->你好!来自明确.
不应该是:?
A->您好!来自明确.
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) 我对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) 在依赖注入方面有明确的实现接口的好处吗?
据我所知,接口可以显式或隐式实现:
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) 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#Boolean struct元数据.
Boolean实现了IConvertible接口.但看看布尔的成员,我看不到大多数IConvertible成员.
我和一些同事做了一些测试,包括创建我们自己的类,并得出结论,IConvertible必须明确地为Boolean实现.
问题是,为什么它们不可见?我理解它可能是一个"按设计决定"但我明白如果检查元数据的任何人都可以看到它会增加更多的价值.
测试在VS2010 .NET4.0中完成
c# ×8
interface ×3
inheritance ×2
compilation ×1
f# ×1
fortran ×1
generics ×1
hashset ×1
icollection ×1
implicit ×1
metadata ×1