我有以下接口及其继承:
public interface IBase1<T> where T : struct
{}
public interface IBase2<T> where T : struct
{
T XMethod();
}
public interface IBaseInt : IBase1<int>, IBase2<int>
{}
public interface IBaseDecimal : IBase1<decimal>, IBase2<decimal>
{}
public interface IBaseInner<T>
{
void InnerMethod();
}
然后我有一个实现IBaseInt和IBaseInner
的类public class BaseInt : IBaseInt, IBaseInner<int>
{
// some implementation code here
public int XMethod()
{
// implementation of int Xmethod
}
public void InnerMethod()
{
// implementation of InnerMethod
}
}
其他,实现IBaseDecimal和IBaseInner
public class BaseDecimal : IBaseDecimal, IBaseInner<decimal>
{
private readonly IBaseInt baseInt; //Injected by ninject
public BaseDecimal(IBaseInt pBaseInt)
{
this.baseInt = pBaseInt;
}
public decimal XMethod()
{
// implementation of decimal XMethod
}
public void InnerMethod()
{
baseInt.XMethod(); // throws an exception, because don't recognize this method
}
}
但如果我通过明确的演员来电话:
(baseInt as IBase2).XMethod();
不抛出异常。
你能解释一下为什么会这样吗?