var fileName = string.Format(@"{0}\{1}\{2}.dll", Path.Combine(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location)),"ProviderDLLS", $"UtilityPayments.Providers.TestProvider");
if (!File.Exists(fileName)) return null;
AssemblyName an = AssemblyName.GetAssemblyName(fileName);
Assembly assembly = Assembly.LoadFile(fileName);
Type objectType = assembly.GetType("UtilityPayments.Providers.TestProvider.Class1");
IProviderProcessor remoteAssembly = (IProviderProcessor)Activator.CreateInstance(objectType);
return await remoteAssembly.GetProviderData();
I call the class method, which implements IProviderProcessor interface using reflection.
if I declare a constructor in Class1 class and add interfaces using DI, I get this error:
System.MissingMethodException: No parameterless constructor defined for type 'UtilityPayments.Providers.TestProvider.Class1'. at System.RuntimeType.CreateInstanceDefaultCtorSlow(Boolean publicOnly, Boolean wrapExceptions, Boolean fillCache) at System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, Boolean wrapExceptions)
at System.Activator.CreateInstance(Type type, Boolean nonPublic, Boolean wrapExceptions)
Class1 constructor:
public class Class1: IProviderProcessor
{
private readonly IProviderService _providerService;
public Class1(IProviderService providerService)
{
_providerService = providerService;
}
}
How can I call Class1 methods using reflection if I want to inject interfaces in the constructor?
The code works fine if I remove the constructor, but I need to use applications other functionalities in Class1, which are implemented using dependency injection.