假设我们有一个接口:
interface ICustomShape
{
}
我们有一个继承自 Shape 类并实现接口的类:
public class CustomIsocelesTriangle : Shape, ICustomShape
{
}
我将如何将 CustomIsocelesTriangle 转换为 ICustomShape 对象,以便在“接口级别”使用?
ICustomShape x = (ICustomShape)canvas.Children[0]; //Gives runtime error: Unable to cast object of type 'program_4.CustomIsocelesTriangle' to type 'program_4.ICustomShape'.