Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8973247
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T18:24:11+00:00 2026-06-15T18:24:11+00:00

public class A { public X x { get; set; } } public class

  • 0
public class A
{
    public X x { get; set; }
}

public class B
{
    public X x { get; set; }
}

public class X
{
    public X(object owner) { /* ... */ }
}

Basically if classes A and B are registered in Windsor I want to be able to resolve X dependency in such a way that it gets instance of the class it was required for.

In plain code it would look like that:

var a = new A();
var x = new X(a);
a.X = x;

Is there a way to do this in Windsor, maybe through some extensibility mechanism?


It looks like some crazy question even for me, so here is some motivation behind it:

The X in the example above is ITracer which is a proxy for TraceSource that is adding some bits of info to each traced message, namely, unique ID of the owner and its type (now it is only ID – that’s why the question – I can’t get to the instance and call GetType() on it).

A brief example to make it more clear. Suppose you have some service IService and want to add traces to it in the most non-intrusive way. But in the application could be dozen of instances of this service, so in traces you want to distinguish them by ID/type. It would be good if class received its tracer from the container and just wrote messages there when needed, without thinking of IDs, concrete TraceSource etc.

I already have some infrastructure that allows me to write like that:

[TracedTo("NameOfTheTraceSource")]
public class Service : IService
{
    public ITracer Tracer { get; set; }
}

And Windsor correctly resolves Tracer to be its own (non-shared with other objects) instance of ITracer pointing to TraceSource with name NameOfTheTraceSource. Moreover, if I add traceAllMethods = true to the attribute – Windsor will automatically add interceptor which will write down each method call on this instance via the same Tracer (and only does this if corresponding TraceSource has some listeners configured – we don’t have to support adding them on the fly). This is just awesome because it doesn’t require anything from the developer of Service and it doesn’t suffer performance degradation when it is not needed, not a bit. And so I’m working to make this even more convenient 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T18:24:12+00:00Added an answer on June 15, 2026 at 6:24 pm

    OK, I think what you want here is a facility. Here’s a simple one (with no error checking) that may be what you’re looking for (or at least point you in the right direction):

    public class XFacility : AbstractFacility
    {
        protected override void Init()
        {
            this.Kernel.ComponentCreated += KernelOnComponentCreated;
        }
    
        private void KernelOnComponentCreated(ComponentModel model, object instance)
        {
            var props =
                instance.GetType().GetProperties().Where(p => p.CanWrite && p.PropertyType == typeof (X));
            if (props.Any())
            {
                var pi = props.First();
                pi.SetValue(instance, new X(instance), null);
            }
        }
    }
    

    Now make sure you add the facility to the container before you do any resolving:

    var container = new WindsorContainer();
    container.AddFacility<XFacility>();
    container.Register(Component.For<A>(),
                        Component.For<B>()
        );
    
    var a = container.Resolve<A>();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My object looks like: public class Template { public string Title {get;set;} public string
Given a domain object: class BirthdayDomain { public DateTime Birthday { get; set; }
I have these objects: public class Class { public Guid Id {get;set;} public string
public class Person{ @OneToMany(orphanRemoval = true) List<Cars> myCars; //Get and Set } public class
Correct me If Im wrong. public class Person{ List<Cars> myCars; //Get and Set }
public class MyClass { public string MyProperty{ get; set; } Now, I would like
public class User { public long Id {get;set;} [References(typeof(City))] public long CityId {get;set;} [????]
public class Job { public string Name { get; set; } public int Salary
public class Mailing { public string To { get; set; } public string AttachmentPath
Public class AbcViewModel { public string native{get; set;} public string other{get; set;} public List<AbcViewModel>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.