Jam*_*unt 10 .net c# reflection optional-parameters
我可以使用Type.InvokeMember通过反射来调用方法,并且它看起来非常健壮,例如处理param数组参数.但由于某种原因,它不处理可选参数.
是否有更好的内置方法来调用考虑可选参数的方法(可能使用DLR)?
在下面的示例中,我们使用两个不返回任何参数的函数调用函数.第二个参数是可选的.
MethodInfo mi = default(MethodInfo);
// Loading the assembly
Assembly reflectionAssemby = Assembly.LoadFile(@"C:\RelectionDLL.dll");
// Get type of class from loaded assembly
Type reflectionClassType = reflectionAssemby.GetType("ReflectionDLL.ReflectionClass");
// Create instance of the class
object objReflection = Activator.CreateInstance(reflectionClassType);
mi = reflectionClassType.GetMethod("pub_Inst_NoReturn_Function");
mi.Invoke(objReflection, new object[] { value1, Type.Missing });
Run Code Online (Sandbox Code Playgroud)