[英]Error Function or interface marked as restricted, or the function uses an Automation type not supported in Visual Basic in VB6
I have an array in C# which has to be initialized in VB.我在 C# 中有一个数组,它必须在 VB 中初始化。 While initializing I am getting an error "Function or interface marked as restricted, or the function uses an Automation type not supported in Visual Basic."
初始化时出现错误“函数或接口标记为受限,或 function 使用 Visual Basic 不支持的自动化类型。”
C#.Net Code: C#.Net 代码:
public class InventoryMasterSearchSettings
{
public string[] PrintLabelsIDetail { get; set; }
}
VB.Net Code: VB.Net 代码:
Public Property PrintLabelsIDetail() As String()
Get
PrintLabelsIDetail = mInventoryMasterSearchSettings.PrintLabelsIDetail
End Get
Set(value As String())
mInventoryMasterSearchSettings.PrintLabelsIDetail = value
End Set
End Property
VB6 VB6
Public Sub ShowPrintLabels(invmast() As String, bShowAvailableInventoryOnlyPar As Boolean, Optional fCalledFromScreen As Form, Optional sVendorIDPar As String, Optional sUPCCodePar As String, Optional sInventoryDescriptionPar As String)
Dim oInventoryMasterSearchSettings As New Shared_Interop.InventoryMasterSearchSettings
oInventoryMasterSearchSettings.PrintLabelsIDetail = invmast 'error on PrintLabelsIDetail
End Sub
Are you sure the type of mInventoryMasterSearchSettings.PrintLabelsIDetail is a standard array?您确定 mInventoryMasterSearchSettings.PrintLabelsIDetail 的类型是标准数组吗? (not a collection, or a arraylist type)
(不是集合,也不是 arraylist 类型)
You could try two things.你可以尝试两件事。
First - use a compiler directive to force the array as a COM "safe" array.首先 - 使用编译器指令将数组强制为 COM “安全”数组。
Public Property PrintLabelsIDetail() As <MarshalAs(UnmanagedType.SafeArray)> String()
Get
PrintLabelsIDetail = mInventoryMasterSearchSettings.PrintLabelsIDetail
End Get
Set(value As String())
mInventoryMasterSearchSettings.PrintLabelsIDetail = value
End Set
End Property
So, above may well help marshal the array() of strings to somthing more compatiable with the COM side from a consuming point of view.因此,从消费的角度来看,上面可能有助于将字符串数组()编组为与 COM 端更兼容的东西。
The other idea?另一个想法? You could do a cast of the type before you return it.
您可以在返回之前对类型进行强制转换。
So, say like this:所以,这样说:
Public Property PrintLabelsIDetail() As <MarshalAs(UnmanagedType.SafeArray)> String()
Get
PrintLabelsIDetail = DirectCast(mInventoryMasterSearchSettings.PrintLabelsIDetail, String())
End Get
Set(value As String())
mInventoryMasterSearchSettings.PrintLabelsIDetail = value
End Set
End Property
Also, if the array of string() type is not initialized, then you might want to ensure that has occurred before you return that array.此外,如果 string() 类型的数组未初始化,那么您可能需要确保在返回该数组之前已经发生。
As a general rule, you CAN pass a array from the COM side to .net, but you USEALLY MUST send that array as byref and NOT byval.作为一般规则,您可以将一个数组从 COM 端传递到 .net,但您必须将该数组作为 byref 而不是 byval 发送。
However, you are going the the other way.但是,您正在走另一条路。 .net --> COM side.
.net --> COM 侧。 A plane jane array of string type usually can go to COM side, and even so without the above marshalas compiler directive.
字符串类型的平面简数组通常可以从 go 到 COM 端,即使没有上述 marshalas 编译器指令。
I would try one or the other idea, or in fact as my 2nd example does, try adding both a marshal compiler directive, and a cast.我会尝试一个或另一个想法,或者实际上就像我的第二个示例一样,尝试添加一个编组编译器指令和一个强制转换。
My spider sense suggests that the data type of PrintLabelsIDetail is a collection, iList, or arrarylist when it need to be a array() of string type.我的蜘蛛感觉表明,当 PrintLabelsIDetail 的数据类型需要是字符串类型的 array() 时,它是一个集合、iList 或 arrarylist。 So, check the data type of PrintLabelsIDetail - I not tested but I don't think COM will even like a arrayList in place of Array.
所以,检查 PrintLabelsIDetail 的数据类型——我没有测试过,但我认为 COM 甚至不会喜欢 arrayList 来代替 Array。 And you would do well to initialize the array before sending it across the COM bridge
你最好在通过 COM 桥发送之前初始化数组
I was having a coffee - the directcast might get you a COM/ActiveX compatible array, but I "think" it may well break the connection.我正在喝咖啡——直接广播可能会给你一个兼容 COM/ActiveX 的数组,但我“认为”它很可能会中断连接。 (the VB6 side would be modifying a copy - it may well not persist in the COM object members) - so try the compiler directive without the directcast suggestion
(VB6 方面将修改副本 - 它很可能不会保留在 COM object 成员中) - 所以在没有直接广播建议的情况下尝试编译器指令
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.