Suppose we have the following interfaces:
public interface IAddIn { string IAddInType(); } public interface ISomeInterface { } public interface ISomeInterface2 { } There are also several classes that implement the IAddIn interface (in various variations)
public class A : IAddIn { public A () { // Do some initialization } } public class B : IAddIn, ISomeInterface { public B(string param1, int param2) { // Do some initialization } } public class C : IAddIn { public C(ISomeInterface param1) { // Do some initialization } } public class C : IAddIn { public C(ISomeInterface2 param1) { // Do some initialization } } The problem is that classes cannot work without initialization, it is also possible that in the future there will be other IAddIn implementations with different parameters.
The client class dynamically loads all IAddIn implementations, depending on its type, creates ISomeInterface or ISomeInterface2 (which also need different parameters) or transfers these parameters directly to the class (for example, as class B , which independently implements ISomeInterface based on parameters).
Is there any mechanism for universal creation of instances?
Update The client class creates several instances of each class (5,30,50) with different parameters, and also creates different ISomeInterface, ISomeInterface2 implementations depending on the external code ISomeInterface, ISomeInterface2 which are already sent to the IAddIn implementation IAddIn
Activator.CreateInstance. And in general, you can take ready-made DI / IOC implementations, specify: is it interesting for you to use your bike or just need to get there? - AK ♦Activator.CreateInstance, you can also get constructors on the type, but with dynamic loading it may turn out that different classes need the same parameters, for example string (as for classB), but it doesn't need the int parameter and therefore only one parameter . The only thing that can be traced is that all classes that implementIAddIncan either accept theISomeinterface, IsomeInterface2, or implement them. In extreme cases, this may be a requirement - Aleksandr Necheukhin