Aveva Marine VBNET 编程系列-封装一个类

由于AM的marapi的大部分类实现了IDisposable接口,所有避免内存过大,用了一般需要dispose下

微软官方的解释:

https://learn.microsoft.com/zh-cn/dotnet/api/system.idisposable?view=net-7.0

 

以下是MarDrafting类的定义,很显然它实现了IDisposable接口

 

Imports System.Reflection

Imports Aveva.Marine.Drafting
Imports Aveva.Marine.UI ' marAPI.dll
Imports Aveva.Marine.Utility ' marAPI.dll
Public Class AmEnvironment
    Implements IDisposable

    Private disposedValue As Boolean
    Public Property DraftApp As New MarDrafting
    Public Property MarUI As New MarUi
    Public Property MarUtility As New MarUtil
    Sub New()

    End Sub
    Protected Overridable Sub Dispose(disposing As Boolean)
        If Not disposedValue Then
            If disposing Then

                Dim t As Type = Me.GetType()
                Dim pros = t.GetProperties().Where(Function(p) p.PropertyType.Namespace IsNot Nothing And p.PropertyType.Namespace.StartsWith("Aveva.Marine."))
                For Each pi As PropertyInfo In pros
                    Dim tp = pi.PropertyType
                    Dim obj As IDisposable = pi.GetValue(Me, Nothing)
                    obj.Dispose()
                Next
                ' TODO: 释放托管状态(托管对象)
            End If

            ' TODO: 释放未托管的资源(未托管的对象)并重写终结器
            ' TODO: 将大型字段设置为 null
            disposedValue = True
        End If
    End Sub

    ' ' TODO: 仅当“Dispose(disposing As Boolean)”拥有用于释放未托管资源的代码时才替代终结器
    ' Protected Overrides Sub Finalize()
    '     ' 不要更改此代码。请将清理代码放入“Dispose(disposing As Boolean)”方法中
    '     Dispose(disposing:=False)
    '     MyBase.Finalize()
    ' End Sub

    Public Sub Dispose() Implements IDisposable.Dispose
        ' 不要更改此代码。请将清理代码放入“Dispose(disposing As Boolean)”方法中
        Dispose(disposing:=True)
        GC.SuppressFinalize(Me)
    End Sub
End Class

使用方法

 

posted @ 2023-09-30 09:29  南胜NanSheng  阅读(69)  评论(0编辑  收藏  举报