Get a class, method and arguments based on a string
<p>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. </p>

<p>I have this method here that checks if an object has a method.</p>

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

<p>Then after that how could I check the parameters that a method takes?</p>

<p>Is this all possible? </p>

<hr>

<p><strong>Answer</strong></p>

<p>To get the class:</p>

<pre><code>public T GetInstance&lt;T&gt;(string namespace, string type)
{
    return (T)Activator.CreateInstance(Type.GetType(namespace + "." + type));
}
</code></pre>

<p>To get the method:</p>

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

...

dynamic controller = GetInstance&lt;dynamic&gt;(controllerString);
if (HasMethod(controller, actionString))
{
    controller.GetType().GetMethod(actionString);
}
</code></pre>

<p>To get the parameters </p>

<pre><code>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;
    }
}
</code></pre>
