Delegate.DynamicInvoke无法使用可选参数?

时间:2019-07-30 08:23:50

标签: c# .net reflection delegates

请查看此代码:

// 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 });

它将获得异常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

请帮助。

1 个答案:

答案 0 :(得分:1)

我猜想java.nio.file.NoSuchFileException: 123.xml at java.base/sun.nio.fs.WindowsException.translateToIOException(WindowsException.java:85) at java.base/sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:103) at java.base/sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:108) at java.base/sun.nio.fs.WindowsFileSystemProvider.newByteChannel(WindowsFileSystemProvider.java:231) at java.base/java.nio.file.Files.newByteChannel(Files.java:370) at java.base/java.nio.file.Files.newByteChannel(Files.java:421) at java.base/java.nio.file.Files.readAllBytes(Files.java:3205) at java.base/java.nio.file.Files.readString(Files.java:3283) at InputBox.Pfad(InputBox.java:26) at InputBox.main(InputBox.java:10) null 的使用中的问题似乎不支持对可选args的动态调用。该示例工作正常

Action<int>