Reputation: 121
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?
Upvotes: 1
Views: 1551
Reputation: 121
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.
Upvotes: 1
Reputation: 29836
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" });
Upvotes: 3