作者:Missluckyyy_879 | 来源:互联网 | 2022-11-24 13:36
我的界面有两个相同名称的实现,如下所示:
interface IDBCaller
{
IEnumerable RetrieveUserList();
void Method1();
void Method2();
}
//First implementation of IDBCaller in project1
class DBCaller : IDBCaller
{
public IEnumerable RetrieveUserList()
{
return new List();
}
public void Method1()
{
//doing something
}
public void Method2()
{
//doing something
}
}
//Second implementation of IDBCaller in project2
class DBCaller: IDBCaller
{
public IEnumerable RetrieveUserList()
{
return null; //Currently returns null but the desire state is to call the RetrieveUserList in Implementation1 from project 1
}
public void Method1()
{
//doing something
}
public void Method2()
{
//doing something
}
}
实现1和2具有相同的RetrieveUserList()
方法.当RetrieveUserList()
从实现2调用时,我希望将调用重定向到实现1中的相同方法.
1> Jens Granlun..:
你可以使用继承,例如:
interface IDBCaller
{
IEnumerable RetrieveUserList();
}
class Implementation1 : IDBCaller
{
public virtual IEnumerable RetrieveUserList()
{
return new List();
}
}
class Implementation2 : Implementation1
{
public override IEnumerable RetrieveUserList()
{
return base.RetrieveUserList();
}
}
或者使用具有RetrieveUserList实现的基类
interface IDBCaller
{
IEnumerable RetrieveUserList();
void Method1();
void Method2();
}
class ImplementationBase
{
public virtual IEnumerable RetrieveUserList()
{
return new List();
}
}
class Implementation1 : ImplementationBase, IDBCaller
{
public void Method1()
{
throw new NotImplementedException();
}
public void Method2()
{
throw new NotImplementedException();
}
}
class Implementation2 : ImplementationBase, IDBCaller
{
public void Method1()
{
throw new NotImplementedException();
}
public void Method2()
{
throw new NotImplementedException();
}
}