gpt4 book ai didi

c# - 将字符串数组从 VB6 传递到 C#.net

转载 作者:太空狗 更新时间:2023-10-29 21:39:19 24 4
gpt4 key购买 nike

如何通过 COM Interop 将 VB6 字符串数组 [Assume, s =Array("a", "b", "c", "d")] 传递给 C# .Net?

我尝试实现将 C# 字符串数组传递给 VB 并将 VB 字符串数组传递给 C#,如下所示 C#->VB 工作正常,但其他方式 (VB=>C#) 给出编译错误,称为

Function or interface marked as restricted, or the function uses an automation type not supported in visual basic

下面是我的代码

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

更新:::::::

We need to pass the array as reference in C# .在接口(interface)和方法上改

void SetArray(ref string[] arrayVal ); //ref added

最佳答案

编码到适当的类型将解决您的问题。注意下面的编码和 ref 关键字更改

void ITest.SetArray([MarshalAs(UnmanagedType.SafeArray, SafeArraySubType=VT_BSTR)] ref string[] arrayVal)
{
string[] stringArray1 = arrayVal;
}

我根据您的代码和您无法从 VB6 获取数据的问题制定了此解决方案。如果上述解决方案对您不起作用,请尝试在此处找到适合您的应用程序的数组类型/子类型 http://msdn.microsoft.com/en-us/library/z6cfh6e6(v=vs.110).aspx

关于c# - 将字符串数组从 VB6 传递到 C#.net,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23507416/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com