【发布时间】:2016-03-17 12:00:47
【问题描述】:
我有一个场景。我正在尝试包装我的代码,以便我拥有根据它们所在的领域实例化自己的通用实体。
就像一个洋葱,它只有一层它自己处理,但同时允许更高级别的人触发内部层开始做某事。它允许我以更好和更可重用的方式打包我的组件,因为我将能够实现安全地传播到所有必需模块的代码,而不会影响可能源自相同基类的更高级别。所以基本上会有一个层次结构,类将共享共性。
简而言之,我在一个接口 (IFoo) 内有一个接口 (IBar) 类型的属性,它继承了另一个接口 (IFooBase),并且我在这个基接口内有另一个属性,它与接口中的属性名称相同以上是其原始(IBarBase)的基本接口的类型
我的问题是我的 Foo 实现,它从 FooBase 调用访问 IBar 属性的方法无法访问 IBarBase,因为该对象未实例化,原因是同时继承的接口执行隐藏属性而不是覆盖。
任何关于如何为 IBarBase 分配 Bar 的实例化对象(实际上是 IBar 的实现并从 IBarBase 派生)的任何建议都将不胜感激,这样我就可以从较低的位置访问该属性执行某些任务的级别。
抱歉,这听起来太复杂了吗?我不确定我是否有任何意义,之前的代码仅供参考。还有一张图片作为说明
public interface IFoo : IFooBase
{
new IBar inst { get; set; }
}
public interface IFooBase
{
IBarBase inst { get; set; }
void SetEventHandlers();
}
public interface IBar : IBarBase
{
int stuff { get; set; }
}
public interface IBarBase
{
int otherStuff { get; set;}
}
public class Foo : FooBase, IFoo
{
public Foo()
{
inst = new Bar();
SetEventHandler();
}
public new IBar inst { get; set; }
}
public class FooBase : IFooBase
{
public void SetEventHandler()
{
inst.otherStuff = 123;
}
public IBarBase inst { get; set; }
}
public class Bar : BarBase, IBar
{
public int stuff { get; set; }
}
public class BarBase :
{
public int otherStuff { get; set;}
}
【问题讨论】:
-
您能否指出您代码中出错的那一行?我很难理解你的设计和问题。
-
当 Foo 在构造函数中调用 SetEventHandler() 时。即使我已经实例化了 inst,但当我尝试分配 inst.otherstuff = 123 时,inst 仍显示为 null。
标签: c# inheritance interface