Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <ul> <li>Use <a href="http://msdn.microsoft.com/en-us/library/system.type.gettype.aspx" rel="noreferrer"><code>Type.GetType(string)</code></a> to get the type object.</li> <li>Use <a href="http://msdn.microsoft.com/en-us/library/system.activator.createinstance.aspx" rel="noreferrer"><code>Activator.CreateInstance(Type)</code></a> to create an instance.</li> <li>Use <a href="http://msdn.microsoft.com/en-us/library/system.type.getmethod.aspx" rel="noreferrer"><code>Type.GetMethod(string)</code></a> to retrieve a method.</li> <li>Use <a href="http://msdn.microsoft.com/en-us/library/system.reflection.methodbase.invoke.aspx" rel="noreferrer"><code>MethodBase.Invoke(object, object[])</code></a> to invoke the method on the object</li> </ul> <p>Example, but with no error checking:</p> <pre><code>using System; using System.Reflection; namespace Foo { class Test { static void Main() { Type type = Type.GetType("Foo.MyClass"); object instance = Activator.CreateInstance(type); MethodInfo method = type.GetMethod("MyMethod"); method.Invoke(instance, null); } } class MyClass { public void MyMethod() { Console.WriteLine("In MyClass.MyMethod"); } } } </code></pre> <p>Each step needs careful checking - you may not find the type, it may not have a parameterless constructor, you may not find the method, you may invoke it with the wrong argument types.</p> <p>One thing to note: Type.GetType(string) needs the assembly-qualified name of the type unless it's in the currently executing assembly or mscorlib.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload