[英]Passing array of class object from C# to 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
尝试将公共字段MyClass.Nome
更改为公共属性:
public string MyNome { get; set; }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.