mvvm light - SimpleIoc (mvvmlight) - how to automatically register all classes implementing a particular interface -


Using SimpleIoc I want to automatically register all the classes that apply to a particular interface. I was unable to see a method on the container of SIMIioc automatically, so I put some code together again through the types of registrations. Unfortunately, the code is not happy (see the commented row).

  var types = AppDomain.CurrentDomain.GetAssemblies (). Selection Exists (s = & gt; s.GetTypes ()). Where (IFoo) .IsAssignableFrom); Voroch (type =. (X => x.IsClass)) {container. Registry & lt; IFoo, type & gt; (); // This line will not be compiled Type can not be sorted by the compiler as type  

I know that there are less elegant ways to enter classes (like only hard Coding "Container. Registrar" to register each class in the interface), but I want to add new implementations without keeping the IoC installer code up to date. It would also be useful, at some point, using the same method for me, able to register classes in other sects.

Any ideas that I have to change to make this creation (or there is no easy

UPDATE Fax posted this comment: / P>

"You can always repeat more types (like you do in your question) and reflection (in your factory category) - (IFoo) Activator.CreateInstance (type); - In fact This is how service locators / ioc containers (in simplified) containers. "

I now have my code in the following Which works, with a warning (should be a parasailable constructor in implementation, and the dependency should be satisfied with the factory so that I am using the IFoo implementation to complete the examples) Factory registration is also included.

Updated code:

  // Get type implementing IFoo ... var type = AppDomain.CurrentDomain.GetAssemblies ( ) .I Lactamony (S = gt; S. GateType ()) where (Typef (IFu). ISSaNableFom); // Register these types and use reflection to prioritize every example ... foreach (type type In there (x = & gt; X.IsClass)) {var t = type; Container. Registrar ((=) => (IFoo) catalyst. Creativity (T), T. toaster ()); } Inject all registered instances of IFoo in IFooFactory when this institute happens. // Note that in instances of IFoo, the runtime should have its own dependency satisfied through the factory before the service is rendered by the factory. Container. Registrar & lt; IFooFactory & gt; (() = & Gt; New FooFactory (Container.Getelli Instance & lt; IFoo & gt; ());;  


Comments

Popular posts from this blog

winforms - C# Form - Property Change -

javascript - amcharts makechart not working -

java - Algorithm negotiation fail SSH in Jenkins -