覆盖接口成员 [英] Overriding interface members
问题描述
我遇到了一个似乎无法解决的问题。在
中:如何覆盖我的基类的接口成员和
从我的派生类中调用重写的方法?
这是我的班级:
类RemoteXmlDataSource:System.Web.UI.WebControls.XmlDataSource
我要覆盖
DataSourceView System.Web.UI.IDataSource.GetView(string viewName)
但我想在我的
之后立即调用重写方法。 />
使用覆盖声明成员,我收到消息
修饰符''覆盖''对此项无效
尝试访问被覆盖的时成员用
(IDataSource)基础).GetView(viewName),我收到消息
使用关键字''base''在此上下文中无效
好的我明白了,我的界面成员完全取代了基类中的成员
。有没有办法做到这一点呢?
我尝试了类似
((IDataSource)(XmlDataSource)这个).GetView(viewNam e)
,但这只是调用我的新成员:)
这使得比仅仅覆盖虚拟方法更加困难
并调用base.X() ......但我希望我没有完全明白;)
亲切的问候,塞巴斯蒂安
" Sebastian" < se ************ @ googlemail.comwrote in message
news:11 ****************** **** @ u2g2000hsc.googlegro ups.com ...
我遇到了一个似乎无法解决的问题。在
中:如何覆盖我的基类的接口成员和
从我的派生类中调用重写的方法?
这是我的班级:
类RemoteXmlDataSource:System.Web.UI.WebControls.XmlDataSource
我要覆盖
DataSourceView System.Web.UI.IDataSource.GetView(string viewName)
但我想在我的
之后立即调用重写方法。 />
使用覆盖声明成员,我收到消息
修饰符''覆盖''对此项无效
您是否将基地声明为公共虚拟基地?
查看此代码是否对您有所帮助:
class Program
{
static void Main(string [] args)
{
B b = new C();
bM();
II i = b;
iM();
控制台.Read();
}
}
class B:II
{
public virtual void M(){Console.WriteLine(" B.M"); }
}
C级:B
{
公共覆盖无效M()
{
Console.WriteLine(" C.M");
}
}
公共接口II
{
void M();
}
Ignacio Machin(.NET / C#MVP)" < machin TA laceupsolutions.com写在
消息新闻:e2 ************** @ TK2MSFTNGP04.phx.gbl ...
" Sebastian" < se ************ @ googlemail.comwrote in message
news:11 ****************** **** @ u2g2000hsc.googlegro ups.com ...
>
我遇到了问题这似乎是不可解决的。在
general:如何覆盖我的基类的接口成员并从我的派生类中调用重写的方法?
这是我的班级:
类RemoteXmlDataSource:System.Web.UI.WebControls.XmlDataSource
我想覆盖
DataSourceView System.Web.UI.IDataSource.GetView(string viewName)
但是我想要在我的
之后立即调用被覆盖的方法。
用覆盖声明成员,我收到消息
修饰符''覆盖''对此无效item
您是否将基地声明为公共虚拟?
看看此代码是否对您有所帮助:
< br $>
class program
{
static void Main(string [] args)
{
B b = new C();
bM();
II i = b;
iM() ;
Console.Read();
}
}
B级:II
{
public virtual void M(){Console.WriteLine(" B.M") ; }
我假设基类方法实现了一个接口,但是没有使用
虚拟关键字(可能会使用它)密封)。
}
class C:B
{
public覆盖void M()
{
Console.WriteLine(" C.M");
}
}
公共接口II
{
void M();
}
事实证明,将显式接口实现声明为虚拟的
是不可能的 - 只有隐式接口实现
支持。似乎是C#语言的限制。或者是否有我不看的
a方式?
看看这个片段:
界面IFace {
string iFaceMethod();
}
公共类BaseClass:IFace {
//有效(隐式接口实现)
public virtual string iFaceMethod(){return" BaseClass public
virtual string iFaceMethod()" ;; }
//有效(显式接口实现 - obove方法不会是
不再是一个隐式接口实现)
string IFace.iFaceMethod(){return" BaseClass string
IFace.iFaceMethod()" ;; }
//修饰符''public''对此项无效
//修饰符''virtual''对此无效item
//公共虚拟字符串IFace.iFaceMethod(){}
}
公共类DerivedClass:BaseClass,IFace {
//工作
公共覆盖字符串iFaceMethod(){
//使用关键字''base''不是在此上下文中有效
// return((IFace)base).iFaceMethod();
//工作
/ / return base.iFaceMethod(); //调用BaseClass public virtual
string iFaceMethod()
return" DerivedClass public override string iFaceMethod()" ;;
}
//有效,但肯定会隐藏base.iFaceMethod
string IFace.iFaceMethod(){
//使用关键字''base''在此上下文中无效
// return((IFace)base).iFaceMethod();
//作品
//返回base.iFaceMethod(); //调用BaseClass public virtual
string iFaceMethod()
//有效,但会导致无限递归
//返回((IFace)this).iFaceMethod();
return" DerivedClass string IFace.iFaceMethod()" ;;
}
//修饰符''public''对此物品无效
//修饰符''覆盖''对此物品无效
//公共覆盖字符串IFace.iFaceMethod(){}
}
亲切的问候,塞巴斯蒂安
Hi,
I''m confronted with a problem that seems not to be solvable. In
general: How can I override an interface member of my base class and
call the overridden method from my derived class?
This is my class:
class RemoteXmlDataSource : System.Web.UI.WebControls.XmlDataSource
And I want to override
DataSourceView System.Web.UI.IDataSource.GetView(string viewName)
but I want to make a call to the overridden method right after my
stuff.
Declaring the member with override, I get the message
The modifier ''override'' is not valid for this item
When trying to access the "overridden" member with
(IDataSource)base).GetView(viewName), I get the message
Use of keyword ''base'' is not valid in this context
Ok I understand, my interface member completely replaces the member
from the base class. Is there any way to do this even though?
I tried something like
((IDataSource)(XmlDataSource)this).GetView(viewNam e)
but that just calls my new member :)
That makes deriving much harder than just overriding a virtual method
and calling base.X()... But I hope I just did not fully understand ;)
Kind regards, Sebastian
Hi,
"Sebastian" <se************@googlemail.comwrote in message
news:11**********************@u2g2000hsc.googlegro ups.com...Hi,
I''m confronted with a problem that seems not to be solvable. In
general: How can I override an interface member of my base class and
call the overridden method from my derived class?
This is my class:
class RemoteXmlDataSource : System.Web.UI.WebControls.XmlDataSource
And I want to override
DataSourceView System.Web.UI.IDataSource.GetView(string viewName)
but I want to make a call to the overridden method right after my
stuff.
Declaring the member with override, I get the message
The modifier ''override'' is not valid for this itemDid you declare the base as public virtual?
See if this code helps you:
class Program
{
static void Main(string[] args)
{
B b = new C();
b.M();
II i = b;
i.M();
Console.Read();
}
}
class B : II
{
public virtual void M() { Console.WriteLine("B.M"); }
}
class C : B
{
public override void M()
{
Console.WriteLine("C.M");
}
}
public interface II
{
void M();
}
"Ignacio Machin ( .NET/ C# MVP )" <machin TA laceupsolutions.comwrote in
message news:e2**************@TK2MSFTNGP04.phx.gbl...Hi,
"Sebastian" <se************@googlemail.comwrote in message
news:11**********************@u2g2000hsc.googlegro ups.com...>Hi,
I''m confronted with a problem that seems not to be solvable. In
general: How can I override an interface member of my base class and
call the overridden method from my derived class?
This is my class:
class RemoteXmlDataSource : System.Web.UI.WebControls.XmlDataSource
And I want to override
DataSourceView System.Web.UI.IDataSource.GetView(string viewName)
but I want to make a call to the overridden method right after my
stuff.
Declaring the member with override, I get the message
The modifier ''override'' is not valid for this item
Did you declare the base as public virtual?
See if this code helps you:
class Program
{
static void Main(string[] args)
{
B b = new C();
b.M();
II i = b;
i.M();
Console.Read();
}
}
class B : II
{
public virtual void M() { Console.WriteLine("B.M"); }I assume that the base class method implements an interface, but doesn''t use
the virtual keyword (that might make it sealed).
}
class C : B
{
public override void M()
{
Console.WriteLine("C.M");
}
}
public interface II
{
void M();
}
It turns out that declaring explicit interface implementations as
virtual is not possible - only implicit interface implementation
support that. Seems to be a limitation of the C# language. Or is there
a way I do not see?
Look at this snippet:
interface IFace {
string iFaceMethod();
}
public class BaseClass : IFace {
// works (implicit interface implementation)
public virtual string iFaceMethod() { return "BaseClass public
virtual string iFaceMethod()"; }
// works (explicit interface implementation - obove method won''t be
anymore an implicit interface implementation)
string IFace.iFaceMethod() { return "BaseClass string
IFace.iFaceMethod()"; }
// The modifier ''public'' is not valid for this item
// The modifier ''virtual'' is not valid for this item
//public virtual string IFace.iFaceMethod() { }
}
public class DerivedClass : BaseClass, IFace {
// works
public override string iFaceMethod() {
// Use of keyword ''base'' is not valid in this context
//return ((IFace)base).iFaceMethod();
// works
//return base.iFaceMethod(); // calls BaseClass public virtual
string iFaceMethod()
return "DerivedClass public override string iFaceMethod()";
}
// works, but would definitely hide base.iFaceMethod
string IFace.iFaceMethod() {
// Use of keyword ''base'' is not valid in this context
//return ((IFace)base).iFaceMethod();
// works
//return base.iFaceMethod(); // calls BaseClass public virtual
string iFaceMethod()
// works, but leads to infinite recursion
//return ((IFace)this).iFaceMethod();
return "DerivedClass string IFace.iFaceMethod()";
}
// The modifier ''public'' is not valid for this item
// The modifier ''override'' is not valid for this item
//public override string IFace.iFaceMethod() { }
}
Kind regards, Sebastian
这篇关于覆盖接口成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!