将类对象数组从C#传递给VB6

时间:2010-12-19 09:59:25

标签: c# vb6

您在C#中有一个项目,它使用方法GetArray()返回MyClass类型的对象列表。 我用regasm ClassLibrary1.dll / codebase / tlb注册这个dll,然后在VB6的项目中添加ClassLibrary1.tlb引用作为下面的代码。 当我在Microsoft Visual Studio 6.0中运行VB6应用程序时它可以工作,但是当我尝试运行vb6 exe时,我遇到运行时错误

using System.Collections.Generic;

using System.Runtime.InteropServices;

namespace ClassLibrary1

{

    [ClassInterface(ClassInterfaceType.AutoDual)]

    [Guid("D878834C-306E-4260-905F-BDEBDF14CBDA")]

    [ComVisible(true)]

    public class MyProjectC

    {

        public MyClass[] GetArray()

        {

            var list = new List<MyClass>

                           {

                               new MyClass {Nome = "A"},

                               new MyClass {Nome = "AB"},

                               new MyClass {Nome = "AC2"},

                               new MyClass {Nome = "D"}

                           };

            return list.ToArray();
        }
    }


    [ClassInterface(ClassInterfaceType.AutoDual)]

    [Guid("D878834C-306E-4260-905F-BDEBDF14C111")]

    [ComVisible(true)]

    public class MyClass

    {

        public string Nome;

    }

}


Private Sub Form_Load()

    On Error GoTo error

    Dim str As String

    Dim oBookMark As Variant
    Dim theProjectC As New ClassLibrary1.MyProjectC


    For Each oBookMark In theProjectC.GetArray
        str = oBookMark.Nome
        MsgBox str
    Next

    Exit Sub

error:
MsgBox "Errore" & Err.Description

End Sub

1 个答案:

答案 0 :(得分:0)

尝试将公共字段MyClass.Nome更改为公共媒体资源:

public string MyNome { get; set; }