Getting the type being constructed when using a factory method

When registering a type to be constructed by a factory method, it is sometimes important to know what type the instance is being constructed for.

For example, when using log4net you might want to inject a logger for that specific type.

I knew how to do it using StructureMap:

For<ILog>().Use(s => LogManager.GetLogger(s.Root.ConcreteType));

But today I had to work out how to do it with Castle. (It’s pretty similar).

Component.For<ILog>().UsingFactoryMethod((k, cm, cc) => LogManager.GetLogger(cc.Handler.Service))

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s