Question

Type.GetType("System.Windows.Forms.MessageBox")
    .GetMethod("Show", new Type[] { Type.GetType("System.String") })
    .Invoke(null, new object[] { "test" });

I'm trying invoke the MessageBox.Show("test") method, using only string values for class and method names. However, I cannot get the type of MessageBox by its name.

Any idea?

Was it helpful?

Solution

You need to specify the full name of the assembly.
(it's located in the GAC - If you have win 7 you can see the assemblies in: %windir%\assembly):

    Type t = Type.GetType("System.Windows.Forms.MessageBox, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089");
    var method = t.GetMethod("Show", new Type[] { Type.GetType("System.String") });
    method.Invoke(null, new object[] { "test" });

OTHER TIPS

        string type = typeof(MessageBox).AssemblyQualifiedName;

        Type.GetType(type).GetMethod("Show", new Type[] { Type.GetType("System.String") }).Invoke(null, new object[] { "test" });

Ok everybody, the AssemblyQualifiedName works.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top