Because the class is sealed you cannot simply inherit a message box to extend it (that would be ideal). But you can use reflection to get an instance without a public constructor. You can then find a Show method and invoke it. There are many overloads of the Show method so you have to know which one you want. I'm going to use the simplest which just shows a message and an OK button.
The code looks like this.
ConstructorInfo ci = typeof(MessageBox).GetConstructors(BindingFlags.NonPublic | BindingFlags.Instance)[0];
MessageBox mb =
(MessageBox)ci.Invoke(null);
MethodInfo mi = typeof(MessageBox).GetMethod("Show", new Type[] { typeof(String) });
mi.Invoke(mb, new object[] { "Hello"
});
And the result is, predictably, this.
No comments:
Post a Comment