Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/330.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 通过引用对象传递到VB dll不工作_C#_Arrays_Object_Com_Ref - Fatal编程技术网

C# 通过引用对象传递到VB dll不工作

C# 通过引用对象传递到VB dll不工作,c#,arrays,object,com,ref,C#,Arrays,Object,Com,Ref,在花了三天时间在谷歌上搜索和阅读MSDN后,新手发布的第一篇帖子: 从C#开始,我尝试调用VB dll中的一个方法,其开头如下: Public Function Correl(action As String, ByRef returns As Object, observation_frequency As Double) As Double(,) Dim left As String = Strings.UCase(Strings.Trim(action)) Dim num As Doubl

在花了三天时间在谷歌上搜索和阅读MSDN后,新手发布的第一篇帖子:

从C#开始,我尝试调用VB dll中的一个方法,其开头如下:

Public Function Correl(action As String, ByRef returns As Object, observation_frequency As Double) As Double(,)
Dim left As String = Strings.UCase(Strings.Trim(action))
Dim num As Double = observation_frequency
Dim objectValue As Object = RuntimeHelpers.GetObjectValue(returns)
Dim num2 As Short
Dim num3 As Short
Dim num4 As Short
Dim array As Double(,)
Dim arg_5D_0 As Short
Dim num5 As Short
' The following expression was wrapped in a checked-statement
num2 = CShort(Information.UBound(CType(objectValue, Array), 1))
num3 = CShort(Information.UBound(CType(objectValue, Array), 2))
num4 = num2 + 1
objectValue = RuntimeHelpers.GetObjectValue(returns)
array = New Double(CInt((num2 + 1)), CInt((num3 + 1)))
arg_5D_0 = 1
num5 = num2
For num6 As Short = arg_5D_0 To num5
    Dim arg_65_0 As Short = 1
    Dim num7 As Short = num3
    For num8 As Short = arg_65_0 To num7
        array(CInt(num6), CInt(num8)) = Conversions.ToDouble(NewLateBinding.LateIndexGet(objectValue, New Object()() = { num6, num8 }, Nothing))
    Next
Next '...........
从C#调用方法时,如下所示

double[] dcloses1 = new double[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
double[,] dcloses2 = new double[,] { { 1, 1 }, { 2, 2 }, { 3, 3 }, { 4, 4 } };
object ocloses = (object)dcloses1; // or dcloses2
double[,] nFullMatrix = corobj.Correl("AVC", ref ocloses, (double)frequency); 
我从第二个参数(ByRef作为对象返回)中得到一个错误,该参数源自第12行(num2=CShort(…),如下所示:

Public Function Correl(action As String, ByRef returns As Object, observation_frequency As Double) As Double(,)
Dim left As String = Strings.UCase(Strings.Trim(action))
Dim num As Double = observation_frequency
Dim objectValue As Object = RuntimeHelpers.GetObjectValue(returns)
Dim num2 As Short
Dim num3 As Short
Dim num4 As Short
Dim array As Double(,)
Dim arg_5D_0 As Short
Dim num5 As Short
' The following expression was wrapped in a checked-statement
num2 = CShort(Information.UBound(CType(objectValue, Array), 1))
num3 = CShort(Information.UBound(CType(objectValue, Array), 2))
num4 = num2 + 1
objectValue = RuntimeHelpers.GetObjectValue(returns)
array = New Double(CInt((num2 + 1)), CInt((num3 + 1)))
arg_5D_0 = 1
num5 = num2
For num6 As Short = arg_5D_0 To num5
    Dim arg_65_0 As Short = 1
    Dim num7 As Short = num3
    For num8 As Short = arg_65_0 To num7
        array(CInt(num6), CInt(num8)) = Conversions.ToDouble(NewLateBinding.LateIndexGet(objectValue, New Object()() = { num6, num8 }, Nothing))
    Next
Next '...........
  • 传递一维数组(dcloses1)时,错误为:“参数‘Rank’对数组无效。”为True,因为它需要一个N:N矩阵,但至少传递了数组

  • 传递二维数组(dcloses2)时,错误为:“值不能为NULL。参数名称:参数‘array’为Nothing。”因此似乎无法识别arraya inb对象

  • 该方法似乎希望在使用GetObjectValue()时将数组包装到对象中,但到目前为止,移交所需矩阵的所有意图都失败了

    以前从Excel VBA调用过此函数,但没有任何问题,但C#variantRapper也没有帮助

    你知道如何为第二个参数构造对象吗?在C#和VB互操作之间有类似的经验吗

    更新
    在过去的一天里,我尝试了所有其他的结构、列表、数组形式,但不知何故没有成功。仍然是相同的错误“值不能为NULL。参数名:参数“array”什么都不是。”。我还可以尝试其他想法吗?

    似乎没有必要使用
    ByRef
    (VB)和
    ref
    (C),只需
    作为对象返回
    就可以了。顺便说一句,C#中的
    变量
    等价物是
    动态的
    。如果我不使用
    ref
    ,编译器会说“必须使用
    ref
    传递第二个参数。”。使用
    dynamic
    没有帮助。没有提到我没有VB dll代码,因此更改方法没有选择。不要将数组强制转换为
    Object
    -只需将数组本身传递到方法中,如下所示:
    double[,]nFullMatrix=corobj.Correl(“AVC”,ref dcloss,(double)frequency)
    。调用
    GetObjectValue
    将返回对象的装箱副本(如果它是值类),否则返回对象本身,如下图所示。问题是该方法调用
    ref object
    ,因此当我尝试传递
    ref double[,]
    w/o强制转换到对象我得到错误
    unvalid参数
    ,无法将
    double[,]
    强制转换到
    对象
    。我同意
    GetObjectValue
    会接受它。有没有办法绕过/更改该方法的签名?