Castle Windsor Map Named Component To Specific Property

Following scenario:

We use the Fluent API to register all components in an assembly and two components tyepof(A) with named keys. Another class B with two properties typeof(A) should get the named components injected.


public class A : IA {} public class B : IB { [Named("first")] public IA First { get; set; } [Named("second")] public IA Second { get; set; } } // ... container.Register(Component.For<IA>().Instance(new A(value1)).Named("first")); container.Register(Component.For<IA>().Instance(new A(value2)).Named("second")); // ... var b = container.Resolve<IB>(); // named instances of A get injected to B using the Named attribute

Is this possible with an attribute like Named or only with the Xml Config?


The standard way to do this in Windsor is using service overrides. In your example, when you register B you'd do it like this:

container.Register(Component.For<IB>().ImplementedBy<B>() .ServiceOverrides(new {First = "first", Second = "second"}));

(there are other ways to express this, check the linked docs)

Using a Named attribute as you propose pollutes the code with unrelated concerns (B should not care about what As get injected)


Here's how you would solve this problem using DependsOn and incorporating the nameof expression (introduced in C# 6.0):

        .Instance(new A(value1))
         .Instance(new  A(value2))
              Dependency.OnComponent(nameof(B.First), "first"),
              Dependency.OnComponent(nameof(B.Second), "second")

Dependency.OnComponent has many overrides, but in this case the first parameter is the property name, and the second parameter is the component name.

See here for more documentation.


  • Django : Model Instance history
  • Client-Side: Accessing Windows Azure Drive?
  • Serializing socket
  • a concept similar to pointers in as3?
  • LNK1104: cannot open file 'kernel32.lib'
  • Curried UDF - Pyspark
  • Invert string in Rust
  • jersey/tomcat Description The origin server did not find a current representation for the target res
  • Less Conflicting Session Manager for Zope 2
  • Magento get URL before current
  • C#: Import/Export Settings into/from a File
  • What does “t” refer to in this SQL?
  • Syntax error on tokens, AnnotationName expected instead - error on query
  • JSR-330 support in Picocontainer : @Inject … @Named(\"xxx)
  • CERN ROOT exporting data to plain text
  • How can I display the parent menu item's description using Wordpress walkers?
  • calculate gradient output for Theta update rule
  • How can I enlarge video fullscreen without the affected interface project in as3?
  • Django simple Captcha “No module named fields” error
  • Installing iPhone App to iPhone
  • Why does access(2) check for real and not effective UID?
  • Typescript - Unable to get 'import' statement to function
  • Exception “firebase.functions() takes … no argument …” when specifying a region for a Cloud Function
  • Highlight one bar in a series in highcharts?
  • Disabling Alt-F4 on a Win Forms NotifyIcon
  • Updating server-side rendering client-side
  • Calling of Constructors in a Java
  • PHP: When would you need the self:: keyword?
  • How to set the response of a form post action to a iframe source?
  • Change div Background jquery
  • Qt: Run a script BEFORE make
  • Easiest way to encapsulate a HTML5 webpage into an android app?
  • Busy indicator not showing up in wpf window [duplicate]
  • reshape alternating columns in less time and using less memory
  • costura.fody for a dll that references another dll
  • Observable and ngFor in Angular 2
  • How to Embed XSL into XML
  • UserPrincipal.Current returns apppool on IIS
  • Conditional In-Line CSS for IE and Others?
  • java string with new operator and a literal