简体   繁体   English

从Visual Basic 6使用.NET类的最佳方法是什么?

[英]What's the best way to use .NET classes from Visual Basic 6?

At my workplace I'm stuck with Visual Basic 6, but after reading the answer to " How do I sort arrays using vbscript? " I got my hopes up, that at least some of the missing VB6 features can be supplemented by .NET features. 在我的工作场所,我坚持使用Visual Basic 6,但在阅读了“ 我如何使用vbscript对数组进行排序? ”的答案后, 我对此抱有希望,至少有一些缺少的VB6功能可以通过.NET功能进行补充。 (Different kinds of collections, mostly) (大多数不同种类的藏品)

It seems, that at least some .NET classes are available for scripting and can be used from VB6 through CreateObject . 看来,至少有一些.NET类可用于编写脚本,可以从VB6到CreateObject But I get a distinct to-good-to-be-true feeling, so... 但是我得到了一种与众不同的感觉,所以......

  • Are there any pitfalls I might encounter? 我可能遇到任何陷阱吗? (Besides that .NET has to be installed) (除了必须安装.NET)
  • Are all .NET classes available through CreateObject ? 是否所有.NET类都可通过CreateObject
  • Is there any way to import those classes into my VB6 project, so I can have IntelliSense? 有没有办法将这些类导入我的VB6项目,所以我可以拥有IntelliSense?
  • Do you have any general suggestions regarding this approach? 您对此方法有任何一般性建议吗?

There's lot's of information available at the VB Fusion Developer Center on MSDN . MSDN上的VB Fusion开发人员中心提供了大量信息。 Of particular interest will be Using the .NET Framework Class Library from Visual Basic 6 and Can I Interest You in 5000 Classes? 特别感兴趣的是使用Visual Basic 6中的.NET Framework类库我可以在5000个类中感兴趣吗? .

You'll need to write some code in .NET to use the techniques described in the articles. 您需要在.NET中编写一些代码才能使用文章中描述的技术。 If you don't already have Visual Studio 2008, you can download Visual Basic 2008 Express for free. 如果您还没有Visual Studio 2008,则可以免费下载Visual Basic 2008 Express

From working on the conversion for my company CAD/CAM application. 从我的公司CAD / CAM应用程序的转换工作。 I do not recommend going from the bottom up ie replacing the core DLL or adding core functionality thru .NET. 我不建议自下而上,即替换核心DLL或通过.NET添加核心功能。

I had much better results working from the top down starting by replacing forms and working my way down into the Core DLL. 从顶部开始,我有更好的结果,从替换表单开始,然后进入Core DLL。

The biggest hurdle was providing a suitable printer and graphics interface. 最大的障碍是提供合适的打印机和图形界面。 But Microsoft release of a Printer Compatibility Kit solved both problems. 但微软发布的打印机兼容性套件解决了这两个问题。 Luckily when .NET was but a glimmer on the horizon I put all the VB6 centric graphic and printing calls behind a interface. 幸运的是,当.NET只是一个闪光点时,我将所有以VB6为中心的图形和打印调用放在一个界面后面。

Again start by replacing the format with a .NET assembly calling your COMM core libraries. 再次首先用调用COMM核心库的.NET程序集替换格式。

If your logic is intertwined with the forms I strongly recommend separating it out into a new COM DLL IN VISUAL BASIC SIX. 如果你的逻辑与表单交织在一起,我强烈建议将它分成一个新的COM DLL IN VISUAL BASIC SIX。 Have the new DLL interact with the Form through a interface. 让新DLL通过接口与Form进行交互。 The form will be a thin shell passing events to the DLL and executing various operation (drawing, re-arranging, etc0. 该表单将是一个瘦shell,将事件传递给DLL并执行各种操作(绘图,重新排列等)。

If you can't avoid using .NET assembly to provide new functionality then create a series of well defined and limited interfaces to control the interaction between the .NET side and the VB6/COM side. 如果你无法避免使用.NET程序集来提供新功能,那么就创建一系列定义良好且有限的接口来控制.NET端和VB6 / COM端之间的交互。

For example we wanted to add office/job management software for our industry to the range of software we sell. 例如,我们希望将我们行业的办公室/工作管理软件添加到我们销售的软件系列中。 We decided to implement using the .NET framework. 我们决定使用.NET框架实现。 It had to interact with our CAD/CAM software and so we create a series of interfaces where each can pull needed information or trigger action from the each other. 它必须与我们的CAD / CAM软件进行交互,因此我们创建了一系列接口,每个接口都可以提取所需信息或触发彼此的操作。

Finally if .NET classes are implement COM Interfaces the big gotcha is that you need to make sure that any parameters that are part of properties in COM must be declared ByVal or .NET will not be able to implement the property and fail to implement the interface. 最后,如果.NET类是实现COM接口,那么最重要的是你需要确保必须声明属于COM属性的任何参数ByVal或.NET将无法实现该属性并且无法实现接口。 This includes the value parameter of a Let or Set 这包括Let或Set的value参数

Maybe this is helpfull: 也许是有帮助的:

By using the available information many of the classes found in the MS .NET mscorlib.dll have been reproduced in Visual Basic 6. This it NOT an implementation of the CLI runtime, only some of the unitlity classes that are in the mscorlib.dll library file. 通过使用可用信息,在MS .NET mscorlib.dll中找到的许多类都已在Visual Basic 6中重现。这不是CLI运行时的实现,只是mscorlib.dll库中的一些unitlity类。文件。

You should only use interop as a mechanism for pushing data between systems - not for core logic code; 您应该只使用interop作为在系统之间推送数据的机制 - 而不是核心逻辑代码; the performance hit of all the marshalling etc will be too much. 所有编组等的性能影响都会太大。 I suggest you look at using the more recent VB variants in VS2008 etc. It is a big shift, but if you can afford to make the change, you won't regret it. 我建议你看看在VS2008等中使用最近的VB变种。这是一个很大的转变,但如果你有能力做出改变,你就不会后悔。

Personally, when I moved from VB6 I switched to C#, but that decision has to be based on local context etc. 就个人而言,当我从VB6迁移到C#时,我决定必须基于本地环境等。

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

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