使用 ByRef 和可选参数从动态对象从 C# 调用 VB6 方法

Calling VB6 method from C# from dynamic object with ByRef and optional Parameters

我通常动态地调用 class 方法没有问题,但只有这个方法不起作用,总是 return 错误:

Dynamic conn = Activator.CreateInstance(Type.GetTypeFromProgID("MyTeam.MyClass"));
bool test = false;
conn.MyFunction(100,"test",DateTime.Now, test, test, "another","another","another");

外部 class 与 VB 的原始方法:

Public Function MyFunction(ByVal Id As Integer, ByVal Var1 As String, ByVal Fecha As Date, 
                                   Optional ByRef Opcion1 As Boolean = False, 
                                   Optional ByRef Opcion2 As Boolean = False, 
                                   Optional ByVal Var1 As String = "", 
                                   Optional ByVal Var2 As String = "",
                                   Optional ByVal Var3 As String = "" ) As String

更新 1:是的,C# 支持 ref/out 参数,但是... "dynamic" 类型的变量调用方法,支持这种类型的输入?

为了在 c# 中传递 byref,当您 调用 函数时,您需要在参数前面加上 ref

conn.MyFunction(100,"test",DateTime.Now, ref test, ref test, "another","another","another");