我有以下类层次结构:
public abstract class BaseClass : IBaseInterface
{
public int PropertyA{
get
{
return this.propertyA;
}
set
{
this.propertyA = value;
// ... some additional processing ...
}
}
}
DerivedClassB : BaseClass
{
// some other fields
}
public class ContainingClassC
{
public IBaseInterface BaseInterfaceObjectD
{
get;
set;
}
}
现在,为了访问 DerivedClassB-Object(继承自 BaseClass)的 PropertyA,我必须将对象强制转换为 BaseClassA 的祖先,如下所示:
// This ContainingClassC is returned from a static, enum-like class:
// containingObject.PropertyA is DerivedClassB by default.
ContainingClassC containingObject = new ContainingClassC();
((IBaseInterface)containingObject.BaseInterfaceObjectD).PropertyA = 42;
有没有办法可以重组这些类以取消演员表?这段代码是图书馆的一部分,我的同事希望我摆脱演员阵容。
目标是简单地编写containingObject.BaseInterfaceObjectD.PropertyA = 42
.