简体   繁体   English

将 .NET ArrayList 编组到 VB6 [Com interop]

[英]Marshal a .NET ArrayList to VB6 [Com interop]

I have a relatively simple C#class that I would like to marshal into a VB project.我有一个相对简单的 C#class,我想将它编组到 VB 项目中。 It looks like this (I simplified a bit for this post):它看起来像这样(我为这篇文章简化了一点):

[Guid("AAAAAAAA-AAAA-AAAA-AAAA-123456789ABC", ClassInterface(ClassInterfaceType.AutoDual), ComVisible(true)]
[ProgId("MyBiz.MyResponse")
[Serializable]

public class MyResponse
{
    public bool Success { get; set; }
    public int ID{ get; set; }
    public string Location{ get; set; }

    public ArrayList Messages { get; set; }
}

Messages contains 0 or more strings.消息包含 0 个或多个字符串。 I compile this and create a type library to be used by VB6.我编译它并创建一个供 VB6 使用的类型库。 Everything work well in terms of data getting passed from the simple types, but the Messages variable, while the VB runtime recognizes it as an ArrayList, does not contain any data in it even when it should.就从简单类型传递的数据而言,一切都运行良好,但是 Messages 变量,虽然 VB 运行时将其识别为 ArrayList,但即使它应该包含任何数据,也不会在其中包含任何数据。 What am I missing, in terms of marshaling the data?在编组数据方面,我缺少什么? I know that generics do not marshal, but I believe an ArrayList does.我知道泛型不会编组,但我相信 ArrayList 会。 Am I missing an attribute, or something else?我错过了一个属性,还是其他什么?

No need to offer alternative solutions, as I am asking this because I want to know how to do it, not because I don't have alternatives if I can get this to work.不需要提供替代解决方案,因为我问这个是因为我想知道如何做到这一点,而不是因为如果我可以让它发挥作用,我没有替代方案。 Thanks!谢谢!

One way to handle this is to use a COM SafeArray to pass data back and forth from .NET to COM.处理此问题的一种方法是使用 COM SafeArray 将数据从 .NET 来回传递到 COM。 I have had better luck with this technique than with a ArrayList.我使用这种技术比使用 ArrayList 运气更好。 The declaration for your Messages could look like:您的消息声明可能如下所示:

[return: MarshalAs(UnmanagedType.SafeArray, SafeArraySubType = VarEnum.VT_BSTR)]
public string[] Messages

This would be seen in VB6 or similar COM client as这将在 VB6 或类似的 COM 客户端中看到为

Public Messages() as String

a COM SafeArray of Strings.字符串的 COM SafeArray。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM