나는 다음 클래스 계층 구조를 가지고, 내가 BaseClassA의 조상에 개체를 캐스팅해야액세스
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 - 객체의 PropertyA에 액세스하려면 (BaseClass로에서 상속을) like :
// 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
으로 작성하는 것입니다.
설명과 코드가 상당 부분 일치하지 않습니다. 하나를 다른 사람과 나란히 가져 오십시오. – Jon
죄송합니다. 작업 중입니다. – lowerkey
'((IBaseInterface) containingObject.BaseInterfaceObjectD)'이미 인터페이스 유형입니다. 너 왜 캐스팅하니? – leppie