如何通过COM互操作将VB6字符串数组 [假设为s =Array("a", "b", "c", "d")] 传递给C# .Net?我尝试了实现将C#字符串数组传递给VB和将VB字符串数组传递给C#,如下所示:C#->VB正常工作,但另一种方式(VB-> C#)会出现编译错误,错误信息是:“函数或接口标记为受限制,或该函数使用Visual Basic不支持的自动化类型”。以下是我的代码:
C#
C#
public interface ITest
{
string[] GetArray();
void SetArray(string[] arrayVal );
}
public class Test : ITest
{
string[] ITest.GetArray() { //Working fine
string[] stringArray = { "red ", "yellow", "blue" };
return stringArray;
}
}
void ITest.SetArray(string[] arrayVal) //Giving an issue
{
string[] stringArray1 = arrayVal;
}
VB
Dim str As Variant
Debug.Print ".NET server returned: "
For Each str In dotNETServer.GetArray 'dotNETServer=TestServer.Test
Debug.Print str
Next
Dim arr(3) As String
arr(1) = "Pahee"
arr(2) = "Tharani"
arr(3) = "Rathan"
dotNETServer.SetArray (arr) 'This one causing the compile error which I mentioned earlier
更新:
在C#中,我们需要将数组作为引用传递。在接口和方法中进行更改。
void SetArray(ref string[] arrayVal ); //ref added