I am writing a Data Acquisition app that uses a wrapper class around HW drivers. My goal is for external users should be able to write their own wrappers for their hardware. It seems assemblies is the way to store these driver wrappers, to be loaded by the app. So I have started with a very simple test code based on a Microsoft example: Loading DLLs at runtime in C# It works when writing to the an assembly method. I added a read method and that does not work. See code:
namespace DLL
{
using System;
public class Class1
{
public void Output(string s)
{
Console.WriteLine(s);
}
public string DriverName() // My added method
{
return "Test";
}
}
}
And here is the main:
namespace ConsoleApplication1
{
using System;
using System.Reflection;
// See: https://stackoverflow.com/questions/18362368/loading-dlls-at-runtime-in-c-sharp
public class Program
{
public static void Main(string[] args)
{
var DLL = Assembly.LoadFile(@"...\Documents\VS\Tests\DLL Test\DLL\bin\Debug\netstandard2.0\DLL.dll");
foreach (Type type in DLL.GetExportedTypes())
dynamic c = Activator.CreateInstance(type);
c.Output("Hello"); // Works
Console.WriteLine(c.DriverName()); // Doesn't work
}
Console.ReadLine();
}
}
}
It gives a compile-time error: Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: ''DLL.Class1' does not contain a definition for 'DriverName''
How come this only works one way but not the other? Is there a simple fix?