如何在 VB6 中以编程方式列出对象的属性? [英] How can I list the properties of an object programmatically in VB6?

查看:23
本文介绍了如何在 VB6 中以编程方式列出对象的属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的一个朋友正在处理一个遗留的 VB6 项目.我已经十年没接触过那种语言了,所以我很生疏.无论如何,是否有任何 类型的 VB6 反射 API?具体来说,他需要一种方法来迭代用户创建的类的属性(和类型).(换句话说,不是外部 COM 对象,而是内部所谓的类模块".)

A friend of mine is working on a legacy VB6 project. I haven't touched that language in ten years, so I'm pretty rusty. Anyway, is there any kind of reflection API for VB6? Specifically, he needs a way to iterate the properties (and types) of a user-created Class. (In other words, not an external COM object, but an internal "Class Module" as it's called.)

如何做到这一点?

推荐答案

Jay 的 answer 是如果您的项目是 ActiveX(ActiveX EXE、DLL 或 OCX,而不是标准 EXE)的方法, 如果课程是公开的.

Jay's answer is the way to go if your project is an ActiveX (ActiveX EXE, DLL, or OCX, as opposed to Standard EXE), and if the classes are public.

但是,您提到您的朋友想要使用内部类模块"来做到这一点.我不确定您指的是私有 .cls 文件(类)还是 .bas 文件(模块),但无论哪种方式,您都只能使用 TypeLib 信息对象库 反映公共 类、用户定义的类型、常量或枚举.

However, you mentioned that your friend wants to do this with "internal class modules". I'm not sure if you are referring to private .cls files (classes), or .bas files (modules), but either way, you can only use the TypeLib Information Object Library to reflect on public classes, user-defined types, constants, or enumerations.

您不能使用库来反映模块、私有类或任何其他声明为私有的内容.

You cannot use the library to reflect modules, private classes, or anything else that is declared private.

作为一般经验法则,在从另一个项目查看项目内容时,您只能对可以在对象浏览器中看到的内容使用反射.也就是说,如果你编译你的项目,创建一个新项目,添加第一个项目作为引用,然后在对象浏览器中查看第一个项目的内容,你在对象浏览器中看到的任何东西都可以通过 访问TypeLib 信息对象库.如果对象浏览器中未列出某些内容,则您将无法使用 TypeLib 信息对象库来反映它.

As a general rule of thumb, you can only use reflection on the things that you can see in the Object Browser when viewing your project's contents from another project. That is, if you compile your project, create a new project, add the first project as a reference, and then view the first project's contents in the Object Browser, anything you can see in the Object Browser can be accessed via the TypeLib Information Object Library. If something is not listed in the Object Browser, then you won't be able to use the TypeLib Information Object Library to reflect it.

VB6 没有任何对运行时反射或自省的内置支持.使用 TypeLib 信息对象库进行反射适用于 ActiveX VB6 项目,因为 ActiveX 项目是在带有嵌入式类型库的 COM 组件中编译的,但如前所述,您只能访问在编译后公开的数据类型组件的类型库.使用对象浏览器是一种快速确定类型库中内容的方法,因为对象浏览器实际上会检查组件的类型库以填充您在对象浏览器中看到的内容,只要您从 查看组件的内容单独的项目(如果您从其自己的 VB6 项目中查看该组件,它将显示公共和私有数据结构,即在 IDE 中可见的所有内容).

VB6 doesn't have any built-in support for run-time reflection or introspection. Using the TypeLib Information Object Library for reflection works for ActiveX VB6 projects because ActiveX projects are compiled in COM components with embedded type libraries, but as mentioned you can only access the data types that are publicly exposed in the compiled component's type library. Using the Object Browser is a quick way to determine what is in the type library because the Object Browser actually inspects the component's type library to populate what you see in the Object Browser, as long as you are viewing the component's contents from a separate project (if you view the component from its own VB6 project, it will display public and private data structures, i.e. everything that is visible in the IDE).

这篇关于如何在 VB6 中以编程方式列出对象的属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆