简体   繁体   English

在Castle Windsor中注入属性(以及相同的重写构造函数设置)

[英]Injecting a property (and overriding constructor setting of same) in Castle Windsor

I have a component with a property I'd like to override under certain circumstances. 在某些情况下,我有一个具有要覆盖的属性的组件。
Short version is-- 简短版本是-

public interface IMyClass
{
  public string SomeValue {get; }
}

public class MyClass : IMyClass
{
   public string SomeValue {get; set;}

   public MyClass()
   {
     //do nothing related to SomeValue
   }

   public MyClass(IHelper helper)
   {
      SomeValue = helper.ReadFromAConfigurationFile();
      //other code follows, unrelated to SomeValue
   }
}

Later on, as suggested here I do.. 后来,作为建议在这里我做..

var classParams= new Dictionary<string, string> { { "SomeValue", "A New Value" } }

    Component.For<IMyClass>()
        .ImplementedBy<MyClass>()
        .DependsOn(classParams)
        .Named("NamedInstanceGoesHere")
        .LifestyleTransient());

Finally, I resolve my component like this: 最后,我这样解析我的组件:

var myComponent= container.Resolve<IMyClass>("NamedInstanceGoesHere");

When I examine myComponent , the value of SomeValue is as it was set in the constructor and not as I tried to override it. 当我审视myComponent ,价值SomeValue是因为它是在构造函数中设置,而不是当我试着将其覆盖。

Is there a way to do what I'm after? 有什么方法可以做我想要的吗? Why IMyClass doesn't have a public setter for SomeValue and MyClass does I'm not sure, but it's probably contributing to my problem... 为什么IMyClass没有一个公共的setter SomeValueMyClass做我不知道,但它可能有助于我的问题...

I tried the code below, which executes fine without triggering the assertion. 我尝试了下面的代码,该代码可以正常执行而不会触发断言。 Even with IHelper registered. 即使已在IHelper中注册。 To me it seems you are probably doing something slightly different. 在我看来,您可能正在做些不同的事情。

Good luck, 祝好运,

Marwijn. Marwijn。

using System.Collections.Generic;
using System.Diagnostics;
using Castle.MicroKernel.Registration;
using Castle.Windsor;

namespace ConsoleApplication1
{
    public class IHelper
    {

    }

    public interface IMyClass
    {
        string SomeValue { get; }
    }

    public class MyClass : IMyClass
    {
        public string SomeValue { get; set; }

        public MyClass()
        {
            //do nothing related to SomeValue
        }

        public MyClass(IHelper helper)
        {
            SomeValue = "hello";
        }
    }

    class Program
    {
        static void Main()
        {

            var container = new WindsorContainer();

            var classParams = new Dictionary<string, string> { { "SomeValue", "A New Value" } };

            container.Register(
              Component.For<IHelper>(),
              Component.For<IMyClass>()
                .ImplementedBy<MyClass>()
                .DependsOn(classParams)
                .Named("NamedInstanceGoesHere")
                .LifestyleTransient());

            var myObject = container.Resolve<IMyClass>("NamedInstanceGoesHere");

            Debug.Assert(myObject.SomeValue == classParams["SomeValue"]);
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM