I have a system where there are commands, for a chat bot, scattered in different part of the code. I find, and register, them all through reflection using this code:
member private this.RegisterCommands() =
let t = typeof<IBotCommands>
t.Assembly.GetTypes()
|> Seq.filter (fun x -> x.GetInterface(t.Name) <> null)
|> Seq.iter (fun x ->
(
let i = (Activator.CreateInstance(x)) :?> IBotCommands
botCommands.[i.Name()] <- i
i.Initialize(this)
)
)
It's in F#, but I think the method calls give away what it does if you're more familiar with C#.
So, I do Assembly.GetTypes, keep the ones where GetInterface has the interface name I'm looking for and then I instantiate them.
This works well, but only within its own assembly. My question is how can I expand this to all assemblies loaded by the app (but the system ones to not waste time)?
Aucun commentaire:
Enregistrer un commentaire