fix(legacy): complete hosted module cleanup

This commit is contained in:
2026-08-29 20:34:39 +08:00
parent 176952d52c
commit 9f5f548dea
15 changed files with 704 additions and 422 deletions
+1 -1
View File
@@ -15,7 +15,7 @@
{
if (disposing)
{
DisposeReportGridViews();
ReleaseReportGridReferences();
if (components != null)
{
components.Dispose();
+7 -114
View File
@@ -12,7 +12,6 @@ using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using Lskj.Control;
using Lskj.Model;
@@ -100,7 +99,7 @@ namespace Lskj.Report
private void OnFrmMainDisposed(object sender, EventArgs e)
{
DisposeReportGridViews();
ReleaseReportGridReferences();
mReportGridLocalizerViews.Clear();
mPreexistingGridLocalizerViews.Clear();
this.FormClosing -= OnClose;
@@ -123,7 +122,7 @@ namespace Lskj.Report
}
}
private void DisposeReportGridViews()
private void ReleaseReportGridReferences()
{
if (mReportGridResourcesReleased)
{
@@ -136,79 +135,10 @@ namespace Lskj.Report
{
this.mControl.ReleaseResourcesForDispose();
}
HashSet<DevExpress.XtraGrid.Views.Base.BaseView> disposedViews =
new HashSet<DevExpress.XtraGrid.Views.Base.BaseView>();
foreach (System.Windows.Forms.Control control in
EnumerateControls(this.mControl))
{
DevExpress.XtraGrid.GridControl gridControl =
control as DevExpress.XtraGrid.GridControl;
if (gridControl != null)
{
try
{
if (gridControl.DataSource != null)
{
gridControl.DataSource = null;
}
}
catch (Exception ex)
{
LogHelper.Instance.WriteError(ex, ResourceKeys.BaseReport);
}
DevExpress.XtraGrid.Views.Base.BaseView[] views =
gridControl.ViewCollection
.Cast<DevExpress.XtraGrid.Views.Base.BaseView>()
.ToArray();
foreach (DevExpress.XtraGrid.Views.Base.BaseView view in views)
{
if (view == null || !disposedViews.Add(view))
{
continue;
}
try
{
view.Dispose();
}
catch (Exception ex)
{
LogHelper.Instance.WriteError(ex, ResourceKeys.BaseReport);
}
}
}
Type controlType = control.GetType();
if (controlType.FullName != null &&
controlType.FullName.StartsWith(
"DevExpress.XtraTreeList.",
StringComparison.Ordinal))
{
try
{
PropertyInfo dataSourceProperty =
controlType.GetProperty(
"DataSource",
BindingFlags.Instance |
BindingFlags.Public);
if (dataSourceProperty != null &&
dataSourceProperty.CanRead &&
dataSourceProperty.CanWrite &&
dataSourceProperty.GetValue(control, null) != null)
{
dataSourceProperty.SetValue(control, null, null);
}
}
catch (Exception ex)
{
LogHelper.Instance.WriteError(ex, ResourceKeys.BaseReport);
}
}
}
ReleaseTrackedGridLocalizerViews(disposedViews);
// 此处不清空数据源,也不主动 Dispose GridView。报表窗口作为控件树
// 的真实所有者会按 WinForms 生命周期释放控件;这里只解除全局
// Localizer 的事件引用,避免已关闭报表继续被全局对象持有。
ReleaseTrackedGridLocalizerViews();
}
private void CaptureReportGridLocalizerViews()
@@ -224,28 +154,8 @@ namespace Lskj.Report
}
}
private int ReleaseTrackedGridLocalizerViews(
HashSet<DevExpress.XtraGrid.Views.Base.BaseView> disposedViews)
private int ReleaseTrackedGridLocalizerViews()
{
foreach (DevExpress.XtraGrid.Views.Base.BaseView view in
mReportGridLocalizerViews)
{
if (view == null ||
(disposedViews != null && !disposedViews.Add(view)))
{
continue;
}
try
{
view.Dispose();
}
catch (Exception exception)
{
LogHelper.Instance.WriteError(exception, ResourceKeys.BaseReport);
}
}
return RemoveGridLocalizerHandlers(mReportGridLocalizerViews);
}
@@ -420,23 +330,6 @@ namespace Lskj.Report
handler.Target is DevExpress.XtraGrid.Views.Base.BaseView;
}
private static IEnumerable<System.Windows.Forms.Control> EnumerateControls(
System.Windows.Forms.Control root)
{
if (root == null)
{
yield break;
}
yield return root;
foreach (System.Windows.Forms.Control child in root.Controls)
{
foreach (System.Windows.Forms.Control nested in EnumerateControls(child))
{
yield return nested;
}
}
}
/// <summary>
/// <para>说明:获取报表明细</para>
/// <para>创建人:龚宇超</para>