c# - How in Castle.Windsor get object of component from IHandler? -


i created custom facility castle.windsor. in short. mark properties of component-classes custom attribute mycomponent. in facility subscribed on kernel.componentregistered. , on component registration check if of component's properties marked attribute. if yes, use value of property component registration too. problem how object of component when have ihandler , string key of object:

public class componentssearcherfacility : abstractfacility {     protected override void init()     {       kernel.componentregistered += oncomponentregistered;     }      private void oncomponentregistered(string key, ihandler handler)     {       var serviceproperties = handler.componentmodel.implementation                                                     .getproperties()                                                     .where(p => attribute.isdefined(p, typeof(mycomponentattribute)))                                                     .toarray();        if (!serviceproperties.any())         return;        var componentobject = ?????;        foreach (var serviceproperty in serviceproperties)       {         var interfaces = findinterfacesrepresentedbyproperty(serviceproperty.propertytype);         var instance = serviceproperty.getvalue(componentobject, null);         kernel.register(component.for(interfaces)                                  .instance(instance)                                  .lifestylesingleton());       }     }   } 

temporarily use such hack:

var componentobject = kernel.resolve(handler.componentmodel.services.first()); 

but although works in general it's job correctly if follow strict rules in classes declaration. , anyway it's bad.

as say, approach restrictive, looks factory component has singleton? think better approach handle in code registering top level component. encapsulate in extension method maintain fluent style of registration , allow reuse. @ point you're registering top level component you'd have access instance , use reflection access properties generically.


Comments

Popular posts from this blog

asynchronous - C# WinSCP .NET assembly: How to upload multiple files asynchronously -

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -