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 9169003
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T15:46:03+00:00 2026-06-17T15:46:03+00:00

I want to load an assembly (its name is stored in a string), use

  • 0

I want to load an assembly (its name is stored in a string), use reflection to check if it has a method called “CustomType MyMethod(byte[] a, int b)” and call it or throw an exception otherwise. I guess I should do something like this, but would appreciate if someone could offer same advice on how best to do it:

Assembly asm = Assembly.Load("myAssembly"); /* 1. does it matter if write myAssembly or myAssembly.dll? */

Type t = asm.GetType("myAssembly.ClassName");

// specify parameters
byte[] a = GetParamA();
int b = GetParamB();

object[] params = new object[2];
params[0] = a;
params[1] = b;

/* 2. invoke method MyMethod() which returns object "CustomType" - how do I check if it exists? */
/* 3. what's the meaning of 4th parameter (t in this case); MSDN says this is "the Object on which to invoke the specified member", but isn't this already accounted for by using t.InvokeMember()? */
CustomType result = t.InvokeMember("MyMethod", BindingFlags.InvokeMethod, null, t, params);

Is this good enough, or are there better/faster/shorter ways? What about constructors, given that these methods are not static – can they simply be ignored?

When invoking void Methods(), is it ok to just write t.InvokeMember(…) or should you always do Object obj = t.InvokeMember(…)?

Thanks in advance.


EDIT
I have provided a working example as a separate answer below.

  • 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-06-17T15:46:04+00:00Added an answer on June 17, 2026 at 3:46 pm

    use reflection to check if it has a method called “CustomType MyMethod(byte[] a, int b)” and call it or throw an exception otherwise

    Your current code isn’t fulfilling that requirement. But you can pretty easily with something like this:

    var methodInfo = t.GetMethod("MyMethod", new Type[] { typeof(byte[]), typeof(int) });
    if (methodInfo == null) // the method doesn't exist
    {
        // throw some exception
    }
    
    var o = Activator.CreateInstance(t);
    
    var result = methodInfo.Invoke(o, params);
    

    Is this good enough, or are there better/faster/shorter ways?

    As far as I’m concerned this is the best way and there isn’t really anything faster per say.

    What about constructors, given that these methods are not static – can they simply be ignored?

    You are still going to have to create an instance of t as shown in my example. This will use the default constructor with no arguments. If you need to pass arguments you can, just see the MSDN documentation and modify it as such.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to load multiple languages to use IVONA SDK with SSML for iPhone.
I want to load two assemblies from C++/CLI; assembly A depends on assembly B,
How do I load a dll and its dependencies? I don't want to place
at first sorry my bad English I want load model in my plugin controller
I want to load the form fields using json data. I am using extjs
I want to load an image via css that stretches to the entire screen.
I want to load up a random image while a page it is loading
I want to load data from MySql database to a HTTP form When I
I want to load PNG-file from resources. There is a roughly MFC-way (with CResourceStream
I want to load multiple variables into one .mat file at the end of

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.