Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3695636
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T04:38:30+00:00 2026-05-19T04:38:30+00:00

Greetings! I’ve been fooling around (a bit) with C# and its assemblies. And so

  • 0

Greetings!
I’ve been fooling around (a bit) with C# and its assemblies. And so i’ve found such an interesting feature as dynamic loading assemblies and invoking its class members. A bit of google and here i am, writing some kind of ‘assembly explorer’. (i’ve used some portions of code from here, here and here and none of ’em gave any of expected results).

But i’ve found a small bug: when i tried to invoke class method from assembly i’ve loaded, application raised MissingMethod exception. I’m sure DLL i’m loading contains class and method i’m tryin’ to invoke (my app ensures me as well as RedGate’s .NET Reflector):

alt text

The main application code seems to be okay and i start thinking if i was wrong with my DLL… Ah, and i’ve put both of projects into one solution, but i don’t think it may cause any troubles. And yes, DLL project has ‘class library’ target while the main application one has ‘console applcation’ target.

So, the question is: what’s wrong with ’em?

Here are some source code:

DLL source:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ClassLibrary1
{
    public class Class1
    {
        public void Main()
        {
            System.Console.WriteLine("Hello, World!");
        }
    }
}

Main application source:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Assembly asm = Assembly.LoadFrom(@"a\long\long\path\ClassLibrary1.dll");

            try
            {
                foreach (Type t in asm.GetTypes())
                {
                    if (t.IsClass == true && t.FullName.EndsWith(".Class1"))
                    {
                        object obj = Activator.CreateInstance(t);
                        object res = t.InvokeMember("Main", BindingFlags.Default | BindingFlags.InvokeMethod, null, obj, null); // Exception is risen from here
                    }
                }
            }
            catch (Exception e)
            {
                System.Console.WriteLine("Error: {0}", e.Message);
            }

            System.Console.ReadKey();
        }
    }
}

UPD: worked for one case – when DLL method takes no arguments:

DLL class (also works if method is not static):

public class Class1
{
    public static void Main()
    {
        System.Console.WriteLine("Hello, World!");
    }
}

Method invoke code:

object res = t.InvokeMember("Main", BindingFlags.Default | BindingFlags.InvokeMethod, null, null, null);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T04:38:31+00:00Added an answer on May 19, 2026 at 4:38 am

    You are likely getting the InvokeMember() arguments wrong. Here’s a sample that works:

    using System;
    using System.Reflection;
    
    class Program {
        static void Main(string[] args) {
            if (args.Length > 0) Console.WriteLine(args[0]);
            else {
                Assembly asm = Assembly.LoadFrom(Assembly.GetEntryAssembly().Location);
                foreach (Type t in asm.GetTypes()) {
                    if (t.IsClass == true && t.FullName.EndsWith(".Program")) {
                        //object obj = Activator.CreateInstance(t);
                        object res = t.InvokeMember("Main",
                            BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.InvokeMethod,
                            null, null,
                            new object[] { new string[] { "Invoked" } });
                    }
                }
            }
        }
    }
    
    • Note how the Main() method isn’t public, thus BindingFlags.NonPublic
    • Note how the Main() method is static, thus BindingFlags.Static
    • For the same reason, pass null for the target parameter
    • For the same reason, CreateInstance isn’t necessary
    • Note how the Main() method takes a string[] argument, you have to pass it to get Reflection to find the correct method overload.

    Follow the same logic for your Main() method:

                    object res = t.InvokeMember("Main",
                        BindingFlags.Public | BindingFlags.Instance | BindingFlags.InvokeMethod,
                        null, obj,
                        new object[] { });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Greetings, currently I am refactoring one of my programs, and I found an interesting
Greetings, I've been working on a web-interface for some hardware that uses an 8-bit
Greetings! I'm working on wrapping my head around LINQ. If I had some XML
Greetings all, I've been told by co-workers that the GET method is different in
Greetings, I'm trying to find a way to 'unbind' a socket from a particular
Greetings. I'm looking for a way to parse a number of XML files in
Greetings, I'm trying to find either a free .NET library or a command-line executable
Greetings, I need a way (either via C# or in a .bat file) to
Greetings all, I'm trying to localize a .NET/C# project. I'm using string resource files
Greetings! I am trying to check directory write-permissions from within a Windows MFC/ATL program

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.