hi all.
i am making a new desktop app, which involves extensions.
since i haven't the time to create a scripting language or implementing an existing one, i've chosen to call specific methods of all dlls in a specific folder.
so i coded a simple extension that implements Discord RPC.
but when i try to run it, a MissingMethodException gets thrown.
Here's the code
Main assembly:
public void InitializeExtensions(string extpath)
{
foreach (string dll in Directory.GetFiles(extpath, "*.dll"))
{
Assemblies.Add(Assembly.LoadFile(dll));
}
List<Assembly> ToRemove = new List<Assembly>(); //All the assemblies that cannot be initialized will be removed from the list of assemblies
foreach (Assembly DLL in Assemblies)
{
foreach (Type type in DLL.GetExportedTypes())
{
try
{
var c = Activator.CreateInstance(type);
type.InvokeMember("Initialize", BindingFlags.InvokeMethod, null, c, new object[] { this });
}
catch (MissingMethodException ex)
{
ToRemove.Add(DLL);
MessageBox.Show(String.Format("Could not initialize the extension {0}: MissingMethodException {1}, Path.GetFileName(DLL.Location), ex.Message), "YouTubeDesktop - Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
foreach (Assembly a in ToRemove)
{
Assemblies.Remove(a);
}
}
And the DLL
//using main assembly
static Form1 Form1;
public static DiscordRpcClient Client;
public static void Initialize(object[] args)
{
Form1 = (Form1)args[0];
Client = new DiscordRpcClient("My_ID");
//Set the logger
Client.Logger = new ConsoleLogger() { Level = LogLevel.Warning };
//Subscribe to events
Client.OnReady += (sender, e) =>
{
Console.WriteLine("Received Ready from user {0}", e.User.Username);
};
Client.OnPresenceUpdate += (sender, e) =>
{
Console.WriteLine("Received Update! {0}", e.Presence);
};
//Connect to the RPC
Client.Initialize();
}