私が持っているのは、プライベート機能とパブリック機能の両方を持つメイン アプリケーションのフォームの束です。各フォームが作成およびロードされたときにアクセスし、更新、コントロールの追加などのためにフォームへの参照を保持するプラグイン アーキテクチャを配置しています。
私たちがやろうとしているのは、このプラグイン アーキテクチャを実装することですが、一部のプラグインはフォームのプライベート関数を呼び出す必要がある場合があります。Type.InvokeMember で試した例を次に示します。
public partial class Form1 : Form
{
Form1()
{
InitializeComponent();
}
private void SayHello()
{
MessageBox.Show("Hello World!");
}
}
別のDLLで...
public class PluginClass
{
Form1 myReferencedForm1;
PluginClass()
{
//Constructor code here...
//Also sets the reference to existing Form1 instance
}
private CallMember()
{
Type t = typeof(Form1); //I guess I can also call 'myReferencedForm1.GetType();' here as well
t.InvokeMember("SayHello",
System.Reflection.BindingFlags.InvokeMethod |
System.Reflection.BindingFlags.NonPublic |
System.Reflection.BindingFlags.Public,
null, myReferencedForm1, new object[] { });
}
}
私は試してみ"SayHello"
ましたが"SayHello()"
、どちらも「MissingMethodException」エラーを返します:
Method 'Form1.SayHello()' not found.
バインダーを作成して使用する必要がありますか? もしそうなら、どうすればいいですか?System.Windows.Forms.Message でこれを簡単に行うことはできますか? もしそうなら、どのように?