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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T12:30:06+00:00 2026-06-18T12:30:06+00:00

If it is possible, how could I check if a class exists based upon

  • 0

If it is possible, how could I check if a class exists based upon a string, and if that class does exist get a new instance of that object.

I have this method here that checks if an object has a method.

public static bool HasMethod(object objectToCheck, string methodName)
{
    Type type = objectToCheck.GetType();
    return type.GetMethod(methodName) != null;
} 

Then after that how could I check the parameters that a method takes?

Is this all possible?


Answer

To get the class:

public T GetInstance<T>(string namespace, string type)
{
    return (T)Activator.CreateInstance(Type.GetType(namespace + "." + type));
}

To get the method:

public static bool HasMethod(object objectToCheck, string methodName)
{
    Type type = objectToCheck.GetType();
    return type.GetMethod(methodName) != null;
} 

...

dynamic controller = GetInstance<dynamic>(controllerString);
if (HasMethod(controller, actionString))
{
    controller.GetType().GetMethod(actionString);
}

To get the parameters

MethodInfo mi = type.GetMethod(methodName);
if (mi != null)
{
    ParameterInfo[] parameters = mi.GetParameters();
    // The parameters array will contain a list of all parameters
    // that the method takes along with their type and name:
    foreach (ParameterInfo parameter in parameters)
    {
        string parameterName = parameter.Name;
        Type parameterType = parameter.ParameterType;
    }
}
  • 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-18T12:30:08+00:00Added an answer on June 18, 2026 at 12:30 pm

    You could use the GetParameters method on the MethodInfo instance you retrieved with the GetMethod method.

    For example:

    MethodInfo mi = type.GetMethod(methodName);
    if (mi != null)
    {
        ParameterInfo[] parameters = mi.GetParameters();
        // The parameters array will contain a list of all parameters
        // that the method takes along with their type and name:
        foreach (ParameterInfo parameter in parameters)
        {
            string parameterName = parameter.Name;
            Type parameterType = parameter.ParameterType;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: How to get a JavaScript Object's Class? In Ruby I could do
One possible solution could be simple HTTP get or post request, but that wouldn't
Could someone tell if it's possible to get an OpenCL code working with both
Possible Duplicate: What does “WARN Could not determine content-length of response body.” mean and
I have a string of class string string str; how can I check if
During testing of a class I discovered that my class constructor could take an
Is it possible in JUnit to assert an object is an instance of a
I was wondering if it was possible to create a class that would serve
Possible Duplicate: Could anyone explain these undefined behaviors (i = i++ + ++i ,
Possible Duplicate: Could anyone explain these undefined behaviors (i = i++ + ++i ,

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.