简体   繁体   English

如何将对象集合从VB6传递到.NET?

[英]How can I pass a collection of objects from VB6 to .NET?

I need to pass a collection of key/value pairs of strings from VB6 to .NET. 我需要将一组键/值对从VB6传递到.NET。 In VB6 code they exist in a native collection. 在VB6代码中,它们存在于本机集合中。 But I am not sure what I can reference in my .NET project to be able to accept a parameter of type Collection in one of my methods. 但是我不确定在.NET项目中可以引用什么,以便能够在我的一种方法中接受Collection类型的参数。

I tried adding a reference to Visual Basic for Applications but got back "A reference to 'Visual Basic For Applications' could not be added." 我尝试添加对Visual Basic for Applications的引用,但返回“无法添加对'Visual Basic For Applications”的引用。

Am I going about it the wrong way? 我会以错误的方式处理吗?

You could use something like this in c#: 您可以在c#中使用如下代码:

[Guid("fb5e929a-2f8b-481e-9516-97edf5099df4")]
[ComVisible(true)]
public interface myInterface{
public void addObject(string key, string value);
}

And in your class, you could have this: 在课堂上,您可以这样:

private collection
public addObject(string key, string value)
{
collection.Add(key, value);
}

This should allow you to call addObject in vb6 and passing your data. 这应该允许您在vb6中调用addObject并传递数据。 Then .net will add it to a collection, so instead of passing your whole collection from vb6 to .net, you pass them one by one. 然后,.net会将其添加到集合中,因此您无需将整个集合从vb6传递到.net,而是将它们逐个传递。

You can read more about the GUID here. 您可以在此处阅读有关GUID的更多信息

More info about COM with an exemple of code between vb6 and c# here. 有关COM的更多信息,请参见vb6和c#之间的示例代码

Rather than using COM , I have found it far simpler to just serialize my data as JSON and send it over the Interop chasm as plain text. 与使用COM相比 ,我发现将数据序列化为JSON并通过Interop鸿沟以纯文本形式发送数据变得更加简单。 I resisted it at first, but it is now my go-to solution. 一开始我拒绝了它,但是现在这是我的首选解决方案。 Give it a try if the "proper" solutions prove frustrating. 如果“正确”的解决方案令人沮丧,请尝试一下。

Try passing it to .NET by making a HashTable in Visual Basic 6.0: 尝试通过在Visual Basic 6.0中创建一个HashTable将其传递给.NET

Set dictionary = Server.CreateObject("System.Collections.HashTable")
With dictionary
    For i = 1 To 100
        .Add Chr(i), "some text value"
    Next
End With

Then in a C# or VB.NET COM exposed class 然后在C#或VB.NET COM公开类中

public string LoadHashTable(Object tDict)
{
   return String.Format("Type : \"{0}\", Count : {1}", ((Hashtable)tDict).GetType().ToString(), ((Hashtable)tDict).Count);
}

An example of a COM exposed class is in Stack Overflow question Building a COM interop library for ASP Classic using 4.0 framework and Visual Studio 2010 COM公开类的一个示例在Stack Overflow问题中,使用4.0框架和Visual Studio 2010为ASP Classic构建COM互操作库

Remember to register it in both x86 and x64: 请记住在x86和x64中都注册它:

%windir%\Microsoft.NET\Framework\v4.0.30319\regasm MyThing.dll /tlb:MyThing.tlb /codebase MyThing

%windir%\Microsoft.NET\Framework64\v4.0.30319\regasm MyThing.dll /tlb:MyThing.tlb /codebase MyThing

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

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