1

我正在尝试找到一种方法来覆盖包含 EnrichWith 的结构映射注册表语句,如下所示(这里是 Registry 类):

public class MyRegistry : Registry
{
    public MyRegistry()
    {
        For(typeof(IMyList<int>)).EnrichWith(x => DecorateMyList(x)).Use(typeof(MyListA<int>));
        For(typeof(IMyList<int>)).Use(typeof(MyListB<int>));

        For<IMyList<string>>().Use<MyListA<string>>();
        For<IMyList<string>>().Use<MyListB<string>>();
    }

    private object DecorateMyList(object o)
    {
        var genericParameters = o.GetType().GetGenericArguments();
        var myListDecoratorType = typeof(MyListDecorator<>).MakeGenericType(genericParameters);

        var decorated = Activator.CreateInstance(myListDecoratorType, new []{o});
        return decorated;
    }
}

public class MyRegistryUser
{
    ObjectFactory.GetInstance<IMyList<string>>(); // Good: Returns an instance of MyListB<string> as expected
    ObjectFactory.GetInstance<IMyList<int>>(); // Bad: Returns an instance of the decorator containing MyListB<int> - my second rule should have overridden the EnrichWith as well. 
}

我认为结构图中存在故障还是我没有看到的东西是否正确?

提前致谢

4

0 回答 0