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

  • SEARCH
  • Home
  • 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 8666143
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T17:39:19+00:00 2026-06-12T17:39:19+00:00

I am trying to invoke function of a class using Reflection (assuming that object

  • 0

I am trying to invoke function of a class using Reflection (assuming that object initialization as no dependency on Function to be invoked) like this

/// <summary>
    /// Calls a static public method. 
    /// Assumes that the method returns a string
    /// </summary>
    /// <param name="assemblyName">name of the assembly containing the class in which the method lives.</param>
    /// <param name="namespaceName">namespace of the class.</param>
    /// <param name="typeName">name of the class in which the method lives.</param>
    /// <param name="methodName">name of the method itself.</param>
    /// <param name="stringParam">parameter passed to the method.</param>
    /// <returns>the string returned by the called method.</returns>
    /// 
    public static string InvokeStringMethod5(string assemblyName, string namespaceName, string typeName, string methodName, string stringParam)
    {
        //This method was created incase Method has params with default values. If has params will return error and not find function
        //This method will choice and is the preffered method for invoking 

        // Get the Type for the class
        Type calledType = Type.GetType(String.Format("{0}.{1},{2}", namespaceName, typeName, assemblyName));
        String s = null;

        // Invoke the method itself. The string returned by the method winds up in s.
        // Note that stringParam is passed via the last parameter of InvokeMember, as an array of Objects.

        if (MethodHasParams(assemblyName, namespaceName, typeName, methodName))
        {
            s = (String)calledType.InvokeMember(
                        methodName,
                        BindingFlags.InvokeMethod | BindingFlags.Public | BindingFlags.Static,
                        null,
                        null,
                        new Object[] { stringParam });
        }
        else
        {
            s = (String)calledType.InvokeMember(
            methodName,
            BindingFlags.InvokeMethod | BindingFlags.Public | BindingFlags.Static,
            null,
            null,
            null);
        }

        // Return the string that was returned by the called method.
        return s;

    }

    public static bool MethodHasParams(string assemblyName, string namespaceName, string typeName, string methodName)
    {
        bool HasParams;
        string name = String.Format("{0}.{1},{2}", namespaceName, typeName, assemblyName);
        Type calledType = Type.GetType(name);
        MethodInfo methodInfo = calledType.GetMethod(methodName);
        ParameterInfo[] parameters = methodInfo.GetParameters();

        if (parameters.Length > 0)
        {
            HasParams = true;
        }
        else
        {
            HasParams = false;
        }

        return HasParams;

    }  

This was taken form here.

Is there any other/better way to do this ??

Can this activity be generalship. Like using Dynamic and can call Non-Static methods in .Net 4.0 so that the return type can be independent.

I have never used dynamic keyword in actual scenario (only read some examples) to actul usage still unknown to me

Any help/direction in this regard would be appreciated
Thanks

  • 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-12T17:39:20+00:00Added an answer on June 12, 2026 at 5:39 pm

    To answer you query on dynamic; no, that wouldn’t be a good fit here. dynamic is useful when the member-name (or operation) is known at compile-time, but is not provable to exist – basically, duck-typing. For example:

    dynamic foo = GetSomeRandomObject();
    foo.ThisShouldExist("abc");
    

    this does similar things, but the usage is different. So yes, you’re left with reflection. What you are doing is pretty-much right. The only thing I might change would be to obtain the MethodInfo, and work from there – although if you can change the API to accept a single string assemblyQualifiedName it would be more convenient and flexible. But perhaps:

    public static string InvokeStringMethod5(string assemblyName,
        string namespaceName, string typeName, string methodName, string stringParam)
    {
        string assemblyQualifiedName = string.Format("{0}.{1},{2}",
            namespaceName, typeName, assemblyName);
        Type calledType = Type.GetType(assemblyQualifiedName);
        if(calledType == null) throw new InvalidOperationException(
            assemblyQualifiedName + " not found");
        MethodInfo method = calledType.GetMethod(methodName,
            BindingFlags.Public | BindingFlags.Static);
        switch (method.GetParameters().Length)
        {
            case 0:
                return (string)method.Invoke(null, null);
            case 1:
                return (string)method.Invoke(null, new object[] { stringParam });
            default:
                throw new NotSupportedException(methodName
                    + " must have 0 or 1 parameter only");
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to invoke a method from another class that means I want to
I'm trying to invoke a method that takes a super class as a parameter
Trying to P/Invoke the SetFileTime function from my C# program, I am using the
I keep getting dialogDiv.dialog is not a function. I'm simply trying to invoke the
I am trying to invoke webservice using jquery ajax call. I am using jasonp
I'm currently trying to invoke a web service from a web application that I've
I'm using platform/invoke and I'm trying to marshal floats LPSTRs and int to a
I am trying to create a generic callback object that will hold arbitrary data
I am getting a C2064 error below below in trying to invoke a function.
I am trying to run the following C function from Java using JNA, but

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.