vb.net 如何以编程方式添加/删除引用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/594239/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 14:05:44  来源:igfitidea点击:

How to Add/Remove reference programmatically?

vb.netms-access

提问by Suman

My Application is built to a scan MS Access database in VB.NET.

我的应用程序是为在 VB.NET 中扫描 MS Access 数据库而构建的。

When the Access application is distributed to end users, they may have different versions of COM components. Is it possible to Add/Remove references programmatically to resolve broken references due to differing versions?

当 Access 应用程序分发给最终用户时,他们可能拥有不同版本的 COM 组件。是否可以通过编程方式添加/删除引用来解决由于版本不同而损坏的引用?

Please share me code or link for reference.

请分享我的代码或链接以供参考。

回答by Fionnuala

Here is some sample code:

下面是一些示例代码:

Create Reference from File

从文件创建参考

  Sub AddWS()
  'Create a reference to Windows Script Host, '
  'where you will find FileSystemObject ' 
  'Reference name: "IWshRuntimeLibrary" '
  'Reference Name in references list: "Windows Script Host Object Model" '
  ReferenceFromFile "C:\WINDOWS\System32\wshom.ocx"
  End Sub

  Function ReferenceFromFile(strFileName As String) As Boolean
  Dim ref As Reference

         On Error GoTo Error_ReferenceFromFile
         References.AddFromFile (strFileName)
         ReferenceFromFile = True

  Exit_ReferenceFromFile:
         Exit Function

   Error_ReferenceFromFile:
         ReferenceFromFile = False
         Resume Exit_ReferenceFromFile
   End Function 

Delete Reference

删除参考

  Sub DeleteRef(RefName)
  Dim ref As Reference

      'You need a reference to remove '
      Set ref = References(RefName)
      References.Remove ref

  End Sub 


You can use the references collection to find if a reference exists.

Reference Exists

参考文献存在

  Function RefExists(RefName)
  Dim ref As Object

     RefExists = False

     For Each ref In References
         If ref.Name = RefName Then
             RefExists = True
         End If
     Next

  End Function

From: http://wiki.lessthandot.com/index.php/Add,_Remove,_Check_References

来自:http: //wiki.lessthandot.com/index.php/Add,_Remove,_Check_References

You may also wish to readhttp://www.mvps.org/access/modules/mdl0022.htm

您可能还希望阅读http://www.mvps.org/access/modules/mdl0022.htm

回答by David-W-Fenton

The best solution is to limit references in your Access MDB to internal Access components. This would be the Access reference, the VBA reference, and the DAO reference. All other outside libraries should be used through late binding. If you're using the File System Object, for instance, instead of this (with a reference to the Windows Script Host Object Model):

最好的解决方案是将 Access MDB 中的引用限制为内部 Access 组件。这将是 Access 引用、VBA 引用和 DAO 引用。所有其他外部库都应通过后期绑定使用。例如,如果您使用的是文件系统对象,而不是这个(参考 Windows 脚本宿主对象模型):

  Dim objFSO As New FileSystemObject

  If objFSO.FolderExists("\d9m09521\WB\") Then
    ...
  End If

you would remove the reference and convert it to this:

您将删除引用并将其转换为:

  Dim objFSO As Object

  Set objFSO = CreateObject("Scripting.FileSystemObject")
  If objFSO.FolderExists("\d9m09521\WB\") Then
    ...
  End If

If you're concerned about the performance hit of initializing the FSO each time you use it, you can cache a reference to it. I usually use a static variable inside a function to return an object like this:

如果您担心每次使用 FSO 时初始化 FSO 的性能下降,您可以缓存对它的引用。我通常在函数内使用静态变量来返回这样的对象:

Public Function FSO() As Object
  Static objFSO As Object

  If objFSO Is Nothing Then
     Set objFSO = CreateObject("Scripting.FileSystemObject")
  End If
  FSO = objFSO
End Function

Now, you might want to get fancy and also be able to tear down the instantiated object, in which case you'd do something like this:

现在,您可能想要变得有趣并且能够拆除实例化的对象,在这种情况下,您可以执行以下操作:

Public Function FSO(Optional bolCloseObject As Boolean = False) As Object
  Static objFSO As Object

  If bolCloseObject Then
     Set objFSO = Nothing
     Exit Function
  End If
  If objFSO Is Nothing Then
     Set objFSO = CreateObject("Scripting.FileSystemObject")
  End If
  FSO = objFSO
End Function

In any event, the whole point is that late binding resolves the location of the outside libraries at runtime and thus won't break, except if the outside library is not installed or not properly registered. With late binding, you can trap for both of those conditions, but with early binding, your whole Access app simply breaks.

无论如何,重点是后期绑定在运行时解析外部库的位置,因此不会中断,除非外部库未安装或未正确注册。使用后期绑定,您可以针对这两种情况设置陷阱,但使用早期绑定,您的整个 Access 应用程序只会中断。