我定义了以下类:
public interface IShapeView
{
void DoSomethingWithShape(Shape shape);
}
public interface IShapeView<T> where T : Shape
{
void DoSomethingWithShape(T shape);
}
public class CircleView : IShapeView<Circle>, IShapeView
{
public void DoSomethingWithShape(Circle shape)
{
MessageBox.Show("Circle:" + shape.Radius);
}
void IShapeView.DoSomethingWithShape(Shape shape)
{
DoSomethingWithShape((Circle)shape);
}
}
public class Circle : Shape
{
public Circle()
{
Radius = 1.0;
}
public double Radius { get; set; }
}
以及以下登记:
container.Register(Component.For<IShapeView<Circle>>().ImplementedBy<CircleView>());
当我只有形状的类型时,是否有一个方法可以调用来解析视图?
或者,是否需要使用反射来创建泛型类型参数以获得所需的IShapeView的正确类型?正在查找类似的内容:
Type shapeType = typeof(Circle);
IShapeView view = (IShapeView) container.SomeResolveMethod(shapeType, typeof(IShapeView<>));