111

I want to do this in C#, but I don't know how:

I have a string with a class name -e.g: FooClass and I want to invoke a (static) method on this class:

FooClass.MyMethod();

Obviously, I need to find a reference to the class via reflection, but how?

Ali
  • 3,373
  • 5
  • 42
  • 54
rutger
  • 2,315
  • 4
  • 16
  • 9

6 Answers6

146

You will want to use the Type.GetType method.

Here is a very simple example:

using System;
using System.Reflection;

class Program
{
    static void Main()
    {
        Type t = Type.GetType("Foo");
        MethodInfo method 
             = t.GetMethod("Bar", BindingFlags.Static | BindingFlags.Public);

        method.Invoke(null, null);
    }
}

class Foo
{
    public static void Bar()
    {
        Console.WriteLine("Bar");
    }
}

I say simple because it is very easy to find a type this way that is internal to the same assembly. Please see Jon's answer for a more thorough explanation as to what you will need to know about that. Once you have retrieved the type my example shows you how to invoke the method.

Community
  • 1
  • 1
Andrew Hare
  • 344,730
  • 71
  • 640
  • 635
116

You can use Type.GetType(string), but you'll need to know the full class name including namespace, and if it's not in the current assembly or mscorlib you'll need the assembly name instead. (Ideally, use Assembly.GetType(typeName) instead - I find that easier in terms of getting the assembly reference right!)

For instance:

// "I know String is in the same assembly as Int32..."
Type stringType = typeof(int).Assembly.GetType("System.String");

// "It's in the current assembly"
Type myType = Type.GetType("MyNamespace.MyType");

// "It's in System.Windows.Forms.dll..."
Type formType = Type.GetType ("System.Windows.Forms.Form, " + 
    "System.Windows.Forms, Version=2.0.0.0, Culture=neutral, " + 
    "PublicKeyToken=b77a5c561934e089");
Stefan
  • 652
  • 5
  • 19
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • 2
    +1 Nicely done - I have added an answer that shows *how* to use the type once you have retrieved it. If you want to, go ahead and merge my example into your answer and I will delete mine. – Andrew Hare Jun 25 '09 at 15:14
  • Given that yours has already been accepted, I suggest we do it the other way round - you add my content to your answer, and I'll delete this one :) – Jon Skeet Jun 25 '09 at 15:52
  • 4
    Just to extend your answer further, if you are not sure what to pass as text to GetType function and you can access this class then look at typeof(class).AssemblyQualifiedName, this will give clear idea. – techExplorer Jan 18 '12 at 11:29
12

A simple use:

Type typeYouWant = Type.GetType("NamespaceOfType.TypeName, AssemblyName");

Sample:

Type dogClass = Type.GetType("Animals.Dog, Animals");
André Voltolini
  • 426
  • 8
  • 14
10

Bit late for reply but this should do the trick

Type myType = Type.GetType("AssemblyQualifiedName");

your assembly qualified name should be like this

"Boom.Bam.Class, Boom.Bam, Version=1.0.0.262, Culture=neutral, PublicKeyToken=e16dba1a3c4385bd"
Atul Chaudhary
  • 3,698
  • 1
  • 31
  • 51
  • 5
    Thank you for explicitly clarifying what the assembly qualified name should look like. – Drew Nov 02 '16 at 20:52
3

Via Type.GetType you can get the type information. You can use this class to get the method information and then invoke the method (for static methods, leave the first parameter null).

You might also need the Assembly name to correctly identify the type.

If the type is in the currently executing assembly or in Mscorlib.dll, it is sufficient to supply the type name qualified by its namespace.

GvS
  • 52,015
  • 16
  • 101
  • 139
2

We can use

Type.GetType()

to get class name and can also create object of it using Activator.CreateInstance(type);

using System;
using System.Reflection;

namespace MyApplication
{
    class Application
    {
        static void Main()
        {
            Type type = Type.GetType("MyApplication.Action");
            if (type == null)
            {
                throw new Exception("Type not found.");
            }
            var instance = Activator.CreateInstance(type);
            //or
            var newClass = System.Reflection.Assembly.GetAssembly(type).CreateInstance("MyApplication.Action");
        }
    }

    public class Action
    {
        public string key { get; set; }
        public string Value { get; set; }
    }
}
Amol Bhor
  • 2,322
  • 1
  • 12
  • 14