【问题标题】:Calling VB6 method from C# from dynamic object with ByRef and optional Parameters使用 ByRef 和可选参数从动态对象从 C# 调用 VB6 方法
【发布时间】:2018-06-19 17:18:21
【问题描述】:

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

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

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:是否可以省略可选参数?

  • 问题2:是否可以通过其他方式调用ByRef参数?

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

【问题讨论】:

  • 错误是什么?
  • 在您的问题中包含错误,以便我们知道您要解决什么问题。
  • 当然,C#支持这种类型的参数,但不支持“动态”对象?函数返回“参数无效”

标签: c# dynamic vb6 activator createinstance


【解决方案1】:

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

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

【讨论】:

  • C# 与引用无缝协作,DYNAMIC 对象封装了翻译这些引用的复杂性。 问题出在 VB6 方法本身!!!解决了。​​
猜你喜欢
  • 2019-11-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-19
  • 1970-01-01
  • 2014-08-22
相关资源
最近更新 更多