In order to able to interact with a class you need to know it's API, i.e., its public members. To achieve this with dynamically loaded assemblies you can declare an interface that its exposed classes implement. You will need an additional assembly (a class library) containing the interface(s).
Project "Contracts" (interfaces)
^
|
+-- Project "A"
^ ^
| |
+-- Project "B"
^ ^
| |
+-- Project "C"
The projects A, B, C must also reference "Contracts"
Example:
// Project "Contracts"
public interface IA
{
void DoA();
}
public interface IB
{
void DoB(IA a);
}
// Project "A"
public class A : IA
{
public void DoA()
{
Console.WriteLine("A");
}
}
// Project "B"
public class B : IB
{
public void DoB(IA a);
{
Console.Write("B calling ");
a.DoA();
}
}
The main project can now load the assemblies dynamically, create objects and cast them to the known interfaces.
See Create a .NET Core application with plugins where it is explained how you can load an assembly at runtime (this got a bit complicated in .NET Core and was easier in .NET Framework). I am not going to repeat all the details here. Once you have loaded the assembly, you can search for types implementing a given interface with
foreach (Type type in assembly.GetTypes()) {
if (typeof(IA).IsAssignableFrom(type))
{
if (Activator.CreateInstance(type) is IA a)
{
a.DoA();
}
}
}
using netCoreA
or usenetCore.ClassA
the type of namespace 'netCoreA' could not be found (are you missing a using directive or an assembly reference ?)
, I gave example with .net core projects but actualy I have my projects in .net framework 4.