Reputation: 409
look this code please :
// this method have a optional parameter
public static void Foo(int a = 3) { }
var del = new Action<int>(Foo);
// pass Type.Missing
del.DynamicInvoke(Type.Missing);
// or
del.DynamicInvoke(new object[] { Type.Missing });
it will get exception System.ArgumentException
:
System.ArgumentException: Missing parameter does not have a default value.
Parameter name: parameters
at System.Reflection.MethodBase.CheckArguments(Object[] parameters, Binder binder, BindingFlags invokeAttr, CultureInfo culture, Signature sig)
at System.Reflection.RuntimeMethodInfo.InvokeArgumentsCheck(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at System.Delegate.DynamicInvokeImpl(Object[] args)
at xxx.btnSetting_Click(Object sender, EventArgs e) in xxx\FmMain.cs:line 106
please help.
Upvotes: 0
Views: 310
Reputation: 23228
I guess that problem in using of Action<int>
, seems like it doesn't support dynamic invocation of optional args. This sample is working fine
// this method have a optional parameter
public static void Foo(int a = 3) { }
delegate void SampleDelegate(int a = 3);
static void Main(string[] args)
{
SampleDelegate del = Foo;
// pass Type.Missing
del.DynamicInvoke(Type.Missing);
}
Upvotes: 1