System.Reflection.TargetParameterCountException: 'パラメーター カウントが一致しません。'
...というエラーが出ましたというお話。
C# フォームアプリケーションです。
フォームを操作していると起きるエラーなのに、エラー発生場所はprogram.cs
のApplication.Run()
になります。
場所 System.Reflection.RuntimeMethodInfo.InvokeArgumentsCheck(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) 場所 System.Delegate.DynamicInvokeImpl(Object[] args) 場所 System.Windows.Forms.Control.InvokeMarshaledCallbackDo(ThreadMethodEntry tme) 場所 System.Windows.Forms.Control.InvokeMarshaledCallbackHelper(Object obj) 場所 System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) 場所 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) 場所 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) 場所 System.Windows.Forms.Control.InvokeMarshaledCallback(ThreadMethodEntry tme) 場所 System.Windows.Forms.Control.InvokeMarshaledCallbacks() 場所 System.Windows.Forms.Control.WndProc(Message& m) 場所 System.Windows.Forms.ScrollableControl.WndProc(Message& m) 場所 System.Windows.Forms.Form.WndProc(Message& m) 場所 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m) 場所 System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) 場所 System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) 場所 System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg) 場所 System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData) 場所 System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context) 場所 System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context) 場所 System.Windows.Forms.Application.Run(Form mainForm) 場所 NullableArgInvoke.Program.Main() (C:\Users\xxx\datas\NullableArgInvoke\Program.cs):行 19
結論をいうと、invoke
の呼び方が悪かったです。
nullableなDateTime型を引数に持つメソッドをinvokeしようとして、nullを渡していたのですが、
実はnew DateTime?()
しないといけなかった、というだけでした。
共有のためにサンプルプログラム書きました↓
success
ボタンを押しても何も起きませんが、failure
ボタンを押すとProgram.cs
でエラーが起きます。
public partial class Form1 : Form { private Button m_successButton; private Button m_failureButton; private TextBox m_textBox; private delegate void displayNumber(int? number); public Form1() { InitializeComponent(); m_successButton = new Button(); m_successButton.Location = new Point(12,12); m_successButton.Text = "success"; m_successButton.Click += SuccessButtonClicked; this.Controls.Add(m_successButton); m_failureButton = new Button(); m_failureButton.Location = new Point(12, 41); m_failureButton.Text = "failure"; m_failureButton.Click += FailureButtonClicked; this.Controls.Add(m_failureButton); m_textBox = new TextBox(); m_textBox.Location = new Point(21, 70); this.Controls.Add(m_textBox); } private void DisplayNumber(int? number) { if (number.HasValue == true) { m_textBox.Text = number.Value.ToString(); } else { m_textBox.Text = string.Empty; } } private void SuccessButtonClicked(object sender, EventArgs e) { BeginInvoke(new displayNumber(DisplayNumber), new int?()); } private void FailureButtonClicked(object sender, EventArgs e) { BeginInvoke(new displayNumber(DisplayNumber), null); } }