mardi 30 juillet 2019

Delegate.DynamicInvoke can't use optional parameter?

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.





Aucun commentaire:

Enregistrer un commentaire