在Castle Windsor中实例化对象时出错

时间:2009-08-28 17:02:49

标签: castle-windsor

我不明白我在本问题底部列出的错误。为什么容器试图转换对象,特别是如果编译器没有错误地执行它?我使用的是v2.0.0.5642。

我确定它在配置中,但我迷路了。我真的很感激任何帮助。

  <component id="cipherMaster" type="Demo.View.UserControls.CipherMaster, Demo.View" />
  <component id="cipherVariation" service="Demo.View.UserControls.CipherMaster, Demo.View"
             type="Demo.View.UserControls.CipherVariation, Demo.View" />
  <component id="presenterVariation" service="Demo.Model.Interfaces.IDemoTypePresenter, Demo.Model"
     type="Demo.Presenter.PresenterVariation, Demo.Presenter" >
    <cipherPanel>${cipherVariation}</cipherPanel>
  </component>

namespace Demo.Presenter
{
    public class PresenterCipherMaster : IDemoTypePresenter
    {
        ...
    }
}

namespace Demo.Presenter
{
    public class PresenterVariation : PresenterCipherMaster
    {
        private readonly CipherVariation _variation;

        public PresenterVariation(IMasterDemo view, CipherMaster cipherPanel, FeaturesVariation features,
            IEncryptionEngine engine):base(view, cipherPanel, features, engine)
        {
            _variationLog = new List<CipherVariationLog>();
             _variation = (CipherVariation) cipherPanel; //<<< Cast error points here line 18
             ...
        }
    }
}

public static class IocContainer
{
    public static T Resolve<T>(string key)
    {
        T presenter = Container.Resolve<T>(key);  //<<< Error occurs here
        return presenter;
    }
}

namespace Demo.View.UserControls
{
    public partial class CipherMaster : UserControl
    {
      ...
    }
}

namespace Demo.View.UserControls
{
    public partial class CipherVariation : CipherMaster
    {
        ...
    }
}

=====================

Castle.MicroKernel.ComponentActivator.ComponentActivatorException was unhandled
  Message="ComponentActivator: could not instantiate Demo.Presenter.PresenterVariation"
  Source="Castle.MicroKernel"
  StackTrace:
  ...
  InnerException: System.Reflection.TargetInvocationException
       Message="Exception has been thrown by the target of an invocation."
       Source="mscorlib"
       StackTrace:
       ...
       InnerException: System.InvalidCastException
            Message="Unable to cast object of type 'Demo.View.UserControls.CipherMaster' to type 'Demo.View.UserControls.CipherVariation'."
            Source="Demo.Presenter"
            StackTrace:
                 at Demo.Presenter.PresenterVariation..ctor(IMasterDemo view, CipherMaster cipherPanel, FeaturesVariation features, IEncryptionEngine engine) in E:\Development\MainStreamDemo\Demo.Presenter\PresenterVariation.cs:line 18
            InnerException:

1 个答案:

答案 0 :(得分:0)

我见过Castle解析其他注册服务 first 的情况,然后使用您指定的参数。在这种情况下,由于您使用具体类型作为参数(CipherMaster)并且已注册,因此它可能使用已注册的组件。

我会尝试为两个控件创建一个接口来实现,或者只是将构造函数上的类型改为“Object”或“UserControl”,这样它就不是一个注册类型。