Jump to content

Type.InvokeMember() not finding any method

Adryzz

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();
        }

 

Developer and student lulw

I mainly use C#, but i also know Java, C/C++, js and i'm learning x86 Assembly

Link to comment
Share on other sites

Link to post
Share on other sites

You're better of looking for this in stackoverflow but to answer your question the problem is that your InvokeMember call is missing some BindingFlags, for this case specifically BindingFlags.Static | BindingFlags.Public.

Link to comment
Share on other sites

Link to post
Share on other sites

ok i'll try that, thanks

Developer and student lulw

I mainly use C#, but i also know Java, C/C++, js and i'm learning x86 Assembly

Link to comment
Share on other sites

Link to post
Share on other sites

2 hours ago, Kalysto said:

You're better of looking for this in stackoverflow but to answer your question the problem is that your InvokeMember call is missing some BindingFlags, for this case specifically BindingFlags.Static | BindingFlags.Public.

i get the same error, "cannot create an abstract class"

Developer and student lulw

I mainly use C#, but i also know Java, C/C++, js and i'm learning x86 Assembly

Link to comment
Share on other sites

Link to post
Share on other sites

2 hours ago, Adryzz said:

i get the same error, "cannot create an abstract class"

On which line is the abstract class related exception thrown, and what is the concrete exception type?

ENCRYPTION IS NOT A CRIME

Link to comment
Share on other sites

Link to post
Share on other sites

On 2/9/2020 at 8:13 PM, straight_stewie said:

On which line is the abstract class related exception thrown, and what is the concrete exception type?

MissingMethodException at createinstance saying "Cannot create abstract class"

Developer and student lulw

I mainly use C#, but i also know Java, C/C++, js and i'm learning x86 Assembly

Link to comment
Share on other sites

Link to post
Share on other sites

7 minutes ago, Adryzz said:

MissingMethodException at createinstance saying "Cannot create abstract class"

I suspect that there is an abstract class in assemblies.

The first thing to do is determine if that is the case.

The MissingMethodException has two properties: ClassName and MemberName, which you can look at in your catch block. Those can tell you what you should be looking for.

If you have the source code for the .DLLs in assemblies, just search them for the returns of the properties mentioned above. Otherwise, run the assemblies through a tool like dotPeek, which is free for all use, and then look for the class/member there.

ENCRYPTION IS NOT A CRIME

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×