Hi have a project in C# that return a list of object of type MyClass with the method GetArray(). I register this dll with regasm ClassLibrary1.dll /codebas开发者_开发百科e /tlb and then I add the ClassLibrary1.tlb reference in a project in VB6 as the code below. When I run the VB6 application inside the Microsoft Visual Studio 6.0 it works but when I try to run the vb6 exe I have a runtime error
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
Try changing your public field MyClass.Nome
to a public property:
public string MyNome { get; set; }
精彩评论