Generics and Com Visible .NET libraries(泛型和 Com Visible .NET 库)
问题描述
我开发了一个 VB.NET 库(部分也是基于 C# 开发的),它严重依赖于从抽象泛型基类继承,我正在尝试找出最佳实践.我很遗憾必须使用框架 3.5 来完成.
I've developed a VB.NET library (partially developed on C# as well) that heavily depends on inheriting from an abstract generic base class, and I'm trying to figure out the best practice for this. I Sadly have to do it using framework 3.5.
Public MustInherit Class MyBaseClass(Of T)
Public Whatever As T
End Class
Public Class MyDerivedClass
Inherits MyBaseClass(Of String)
Private _myProperty As String
Public Property MyProperty As String
Get
Return _myProperty
End Get
Set(value As String)
_myProperty = value
End Set
End Property
End Class
我附加 .tlb 文件作为 VBA 中的参考(使用 Excel),然后运行以下代码:
I attach the .tlb file as a reference in VBA (using Excel), and I run the following code:
Dim m As New VBtoVBA.MyDerivedClass
m.MyProperty = "foo"
我收到错误运行时错误 430:类不支持自动化或不支持预期的接口".
And I get the error "Run-time error 430: Class does not support Automation or does not support expected interface".
另一方面,我将第一行更改为:
On the other hand, I change the first lines to:
Public MustInherit Class MyBaseClass
Public Whatever As String
End Class
Public Class MyDerivedClass
Inherits MyBaseClass
VBA 脚本有效.因此,我认为问题出在泛型上(正如其他来源中所记录的那样).但是,删除我的库的通用特性是不可能的.我能想到的最佳"解决方法是编写第三个类,其中包含 MyDerivedClass 作为一个字段,并作为它的非泛型接口工作:
The VBA script works. Hence I assume the issue is with generics (as documented in other sources as well). Dropping the generic feature of my library is not possible, though. The "best" workaround I can think of is to write a third class that includes MyDerivedClass as a field, and works as a non-generic interface to it:
Public Class MyDerivedClassString
Private _innerObj As New MyDerivedClass
Public Property MyProperty As String
Get
Return _innerObj.MyProperty
End Get
Set(value As String)
_innerObj.MyProperty = value
End Set
End Property
Public Property Whatever As String
Get
Return _innerObj.Whatever
End Get
Set(value As String)
_innerObj.Whatever = value
End Set
End Property
End Class
这样我就可以像在 VBA 中那样使用它:
This way I can work with it pretty much as I'd like to in VBA:
m.Whatever = "wha"
MsgBox (m.Whatever)
顺便说一句,我认为可能有另一种(更好的)方法来实现相同的结果,我真的希望如此,因为 m 库由几十个类组成.
By the way I think that there might be another (better) way to achieve the same result, and i really hope so since m library is made up of dozens of classes.
非常感谢.
推荐答案
正如我在 MS Office 应用程序的评论编写库 (dll) 中提到的,有点……硬编码.此 dll 必须公开方法和属性才能在 COM 自动化中使用它.为了能够实现这一点,您需要编写接口:
As i mentioned in comment writing library (dll) for MS Office applications is bit... hard-coded. This dll must exposes methods and properties to be able to use it in COM automation. To be able to achieve that, you need to write interface(s):
Namespace VBtoVBA
Public Interface IMyDerivedClass
Property MyProperty As String
End Interface
End Namespace
然后在 DerivedClass 中
then in DerivedClass
Public Class MyDerivedClass
Inherits MyBaseClass(Of String)
Implements IMyDerivedClass
Private _myProperty As String
Public Property MyProperty As String Implements IMyDerivedClass.MyProperty
现在,转到 Project Properties
窗口1) 选择 Application
选项卡 - 点击 Assembly Information
按钮并在下一个窗口中选择 Make assembly COM visible
复选框(使用 应用设置确定
按钮),
Now, go to Project Properties
window
1) choose Application
tab - click on Assembly Information
button and in the next window select Make assembly COM visible
checkbox (apply setting using OK
button),
2) 选择 Compile
选项卡 - 选择 Register for COM interop
复选框
2) choose Compile
tab - select Register for COM interop
checkbox
3) 保存项目并构建 dll
3) Save project and Build dll
4) 现在,转到 Office 应用程序中的 VBA 代码编辑器 -> References
菜单.在 Reference window
添加对 yourDllName.tlb
4) Now, go to VBA Code editor in Office application -> References
menu. In Reference window
add reference to yourDllName.tlb
现在,您可以在 Office 应用程序中使用您的 dll 了;)
Now, you can use your dll in Office application ;)
我测试过代码:
Option Explicit
Sub DoSomething()
Dim m As VBtoVBA.MyDerivedClass
Set m = New VBtoVBA.MyDerivedClass
m.MyProperty = "Something"
MsgBox m.MyProperty
End Sub
它也有效;)
这篇关于泛型和 Com Visible .NET 库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:泛型和 Com Visible .NET 库
- 在 C# 中异步处理项目队列 2022-01-01
- CanBeNull和ReSharper-将其用于异步任务? 2022-01-01
- C# 通过连接字符串检索正确的 DbConnection 对象 2022-01-01
- 在 LINQ to SQL 中使用 contains() 2022-01-01
- Azure Active Directory 与 MVC,客户端和资源标识同一 2022-01-01
- 是否可以在 .Net 3.5 中进行通用控件? 2022-01-01
- Windows 喜欢在 LINUX 中使用 MONO 进行服务开发? 2022-01-01
- 为什么 C# 中的堆栈大小正好是 1 MB? 2022-01-01
- 带问号的 nvarchar 列结果 2022-01-01
- 使用 rss + c# 2022-01-01