首页 > 解决方案 > 在VB6中创建对象以与用c#编写的dll(tlb)中的对象一起使用?

问题描述

所以我在 vb6 方面的知识相当有限,但这是必需的,因为它是旧代码。

我在 c# 中创建了一个带有对象(字符串类)的 dll。

在 vb6 中使用它的最佳方法是什么?有没有办法可以将它公开为接口,或者在 vb6 中创建一个对象来匹配?

我已经搜索了 MSDN,但该主题的内容非常有限,而不是一个常见的问题。

通常我通过接口公开所有内容,但例如:

class myClass (string x, string y, string z);

不起作用(接口不能包含字段)。

我也试过

myClass(string x, string y, string z); 

我得到一个错误方法必须有一个返回类型。

最后我试过了

myClass iClass(string x, string y, string z);

它返回一个不实现接口成员。

我的课

public string x {get;set;}
public string y {get;set;}
public string z {get;set;}

我无法通过搜索找到任何措辞准确的问题(也许我的措辞在标题中是关闭的,因为我的 vb6 知识又是最低限度的)。

所有引用的代码都来自 c# dll。

标签: c#vb6vb6-migration

解决方案


您可以为您的类提供一个 COM 可调用的包装器,VB6 可以与之互操作。

这是您的类,使用可从 VB6 调用的 COM 可调用包装器进行包装:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices;

namespace CCW
{
    [ClassInterface(ClassInterfaceType.AutoDual)]
    [ComVisible(true)]
    public class myClass
    {
        public string x { get; set; }
        public string y { get; set; }
        public string z { get; set; }
    }
}

推荐阅读