2009-08-28 9 views
0

この質問の最後に記載されているエラーは理解できません。コンテナがオブジェクトをキャストしようとしているのはなぜですか?特にコンパイラがエラーなく実行している場合私はv2.0.0.5642を使用しています。Castle Windsorでオブジェクトをインスタンス化する際のエラー

私はそれが設定されていると確信していますが、私は迷っています。私は本当に助けていただければ幸いです。城は最初に登録されている他のサービスに対して解決され、その後、あなたが指定するパラメータを使用してどこ

<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: 

答えて

0

私は状況を見てきました。この場合、引数(CipherMaster)として具体的な型を使用していて、それが登録されているので、おそらく登録されたコンポーネントを使用しています。

2つのコントロールを実装するインターフェイスを作成するか、コンストラクタのタイプを "Object"または "UserControl"に変更して、登録されたタイプでないようにします。

関連する問題