I have c# library that referenced in main project. The library
- Gets main project assembly;
- Retrieves all types using
System.Reflection
; - Should creates type using
Activator.CreateInstance
(I’m not sure this is the best way).
The library doesn’t know anything about main project, only a metadata that can be obtained through reflection. How the dependencies can be resolved?
private readonly Assembly _assembly;
public Injector()
{
_assembly = Assembly.GetEntryAssembly();
}
public List<string> GetTypes()
{
return _assembly
.GetTypes()
.Select(x => x.FullName)
.ToList();
}
public object GetType(string typeName)
{
Type type = _assembly
.GetTypes()
.First(x => x.FullName == typeName);
object instance = Activator.CreateInstance(type);
return instance;
}
Possible issue: different IoC containers (third-party libraries, own-written).
What is the best way to handle this problem and keep library more automatic without forcing users provide a lot of settings? If it's not possible, could you please offer any other solutions? Thanks.
EDIT: How to provide dependencies to instance in Activator.CreateInstance
or create instance directly from main(source) project? It should be allowed to create any instance that contains in main project. Yes, main project also doesn’t know anything about library. So, it’s desirable to make minimum code changes in main project.
EDIT 2: The library won't be used in source project, it will have own UI interface. For example, Swagger API