Get the corresponding View for a type from Windsor
I have the following classes defi开发者_开发问答ned:
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; }
}
And the following registration:
container.Register(Component.For<IShapeView<Circle>>().ImplementedBy<CircleView>());
Is there a method that I can call to resolve a view when I only have the Type of the shape? Or do I need to go to the trouble of using reflection to create the generic type arguments to get the correct type of IShapeView that I want? Looking for something like this:
Type shapeType = typeof(Circle);
IShapeView view = (IShapeView) container.SomeResolveMethod(shapeType, typeof(IShapeView<>));
There's no method built in because it's not needed. The reason why it's not needed is that in real application you never would pull the component directly from the container - you'd use a typed factory instead.
Typed factory can be easily taught to deal with this scenario. Have a look at this post.
精彩评论