如何通过COM Interop将VB6字符串数组[假设,s =数组(“a”,“b”,“c”,“d”)]传递给C#.Net? 我试图将C#字符串数组传递给VB,将VB字符串数组传递给C#,如下所示C# – VB工作正常但其他方式(VB =
我试图将C#字符串数组传递给VB,将VB字符串数组传递给C#,如下所示C# – > VB工作正常但其他方式(VB => C#)给出一个编译错误,称为“函数或接口标记为受限制,或者function使用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#.在界面和方法中更改它
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