views:

138

answers:

1

How can I use my Windsor container to check if an instance (not just a component) has been registered?

ie. container.ContainsInstance(typeof(MyType))

[EDIT]

Another way of writing this might be

    Kernel.GetAssignableHandlers(typeof(object))
    .Where(handler => handler.Service == typeof(MyType) || handler.ComponentModel.Implementation == typeof(MyType))
    .Any(handler => handler.***Instance*** != null)

Note that the property Instance doesn't exist in the API.

Thanks.

+1  A: 

Officially there's no way to check that. The container is completely unaware of activation mechanism used to construct the instance, and it's a good thing.

Unofficially if you don't care about depending on... non-official solution, you can check if components activator is of type (from the top of my head) ExternalInstanceActivator. If it is, than the component has instance provided from the outside.

More importantly though - Why do you want that information?

Krzysztof Koźmic
Thanks! I have an interface which wraps access to the container, to encapsulate the Castle specific code. I have a Registration class that a caller can use to register components which an adapter turns into a ComponentRegistration. The Registration class has a property called Instance on it, for a singleton instance. The interface has a method GetRegistrations(), which should return all the registrations from the container (with the populated Instance property).
JeffN825

related questions