I had a similar need to generate objects on the fly. In my case, I wanted to
automatically create file watchers at run time for each file we wanted to
watch for. Try this and see if it works for you

Register the following two components in the container
container.Register(Component.For<IExporterFactory>().AsFactory(x =>
x.SelectedWith(new ExporterSelector())));
container.Register(Component.For<IMyExporter>().ImplementedBy<MyExporterJson
>().Named("exporter.json").LifeStyle.Transient);
container.Register(Component.For<IMyExporter>().ImplementedBy<MyExporterXML
>().Named("exporter.xml").LifeStyle.Transient);

Create an interface called IExporterFactory
public interface IExporterFactory
{
    IMyExporter Create(string exporterType);
}

Then create a class that inherits from DefaultTypedFactoryComponentSelector
    public class ExporterSelector: DefaultTypedFactoryComponentSelector
    {
        protected override string GetComponentName(MethodInfo method,
object[] arguments)
        {
            if (method.Name == "Create" && arguments.Length == 1)
            {
                var exporterType = Convert.ToString(arguments[0]).ToLower();

                return "exporter.{0}".FormatWith(exporterType);
            }

            return base.GetComponentName(method, arguments);
        }
    }

You can then get the exporters by calling
container.resolve<IExporterFactory>().Create("xml")
or
container.resolve<IExporterFactory>().Create("json")

I believe that will do what you need it to do, but i don't have a compiler
available to me at the moment so I can't verify. Let me know if you need any
more help.


Matthew Brubaker
[email protected]
(785) 215-7061

-- 
You received this message because you are subscribed to the Google Groups 
"Castle Project Users" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/castle-project-users?hl=en.

Reply via email to