Ошибка создания объекта в замке Виндзор - PullRequest
0 голосов
/ 28 августа 2009

Я не понимаю, какую ошибку я получаю, как указано в нижней части этого вопроса. Почему контейнер пытается привести объект, особенно если компилятор делает это без ошибок? Я использую 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 голосов
/ 28 августа 2009

Я видел ситуации, когда Castle разрешает сопоставление с другими зарегистрированными службами сначала , а затем использует указанные параметры. В этом случае, поскольку вы используете конкретный тип в качестве аргумента (CipherMaster) и он зарегистрирован, он, вероятно, использует зарегистрированный компонент.

Я бы попытался либо создать интерфейс для реализации двух элементов управления, либо просто изменить тип конструктора на «Object» или «UserControl», чтобы он не был зарегистрированным типом.

...