From e57938fbda9bff702004038f76d009422968bff5 Mon Sep 17 00:00:00 2001
From: alielie <alielie@git.com>
Date: 星期五, 06 十一月 2026 18:05:07 +0800
Subject: [PATCH] 提交代码
---
DeviceService/DriverManager/DriverDispatchProxy.cs | 84 ++++++++++++++++++++++++++++++++++++++++++
1 files changed, 84 insertions(+), 0 deletions(-)
diff --git a/DeviceService/DriverManager/DriverDispatchProxy.cs b/DeviceService/DriverManager/DriverDispatchProxy.cs
new file mode 100644
index 0000000..183e096
--- /dev/null
+++ b/DeviceService/DriverManager/DriverDispatchProxy.cs
@@ -0,0 +1,84 @@
+using System.Reflection;
+
+namespace DeviceService.Driver
+{
+ /// <summary>
+ /// 鍩轰簬 DispatchProxy 鐨勯┍鍔ㄥ姩鎬佷唬鐞嗭紙.NET Framework 4.7.1+ 鍐呯疆 API锛夈��
+ ///
+ /// 鐢ㄦ硶锛氬涓昏嚜瀹氫箟涓�涓�"楦瓙鎺ュ彛"锛堟柟娉曞悕銆佸弬鏁扮被鍨嬩笌椹卞姩鏂规硶涓�涓�瀵瑰簲锛夛紝
+ /// 椹卞姩鏃犻渶瀹炵幇璇ユ帴鍙c�佹棤闇�寮曠敤瀹夸富绋嬪簭闆嗐�傛帴鍙h皟鐢ㄦ寜"鏂规硶鍚� + 鍙傛暟涓暟"
+ /// 鍖归厤鍒伴┍鍔ㄦ柟娉曪紝缁� <see cref="DriverInstance"/>锛圡BR 閫忔槑浠g悊锛夎浆鍙戝埌
+ /// 椹卞姩鎵�鍦ㄧ殑 AppDomain 鍐呭弽灏勬墽琛屻��
+ ///
+ /// 涓� RealProxy 鐨勫尯鍒細
+ /// - 鐢熸垚鐨勪唬鐞嗘槸瀹夸富鍩熷唴鐨勬櫘閫氬璞★紝涓嶅啀鏈� Remoting 鐩稿叧鐨勬�紓琛屼负
+ /// 锛圤bject.GetType/ToString 绛夊熀绫绘柟娉曚笉浼氳璇矾鐢憋紝鏃犻渶鎷︽埅锛夛紱
+ /// - out/ref 鍙傛暟閫氳繃鐩存帴鏀瑰啓 args 鏁扮粍鍥炲~锛�
+ /// - 寮傚父鐩存帴鎶涘嚭锛屼笉鍐嶇粡杩� ReturnMessage 鍖呰锛�
+ /// - API 鍦� .NET Core / .NET 5+ 鍚屾牱鍙敤锛屽涓绘湭鏉ュ崌绾у彲鏃犵紳杩佺Щ銆�
+ ///
+ /// 绾︽潫锛歴
+ /// - T 蹇呴』鏄帴鍙o紙DispatchProxy 鍙拡瀵规帴鍙g敓鎴愪唬鐞嗙被锛夛紱
+ /// - 鎺ュ彛鏂规硶鐨勫弬鏁�/杩斿洖鍊煎繀椤诲彲璺ㄥ煙灏侀�侊紙鍩哄厓绫诲瀷銆乻tring銆乕Serializable] DTO锛夈��
+ /// </summary>
+ // 娉ㄦ剰锛氫笉鑳� sealed 鈥斺�� DispatchProxy 鍦ㄨ繍琛屾椂鐢熸垚鍏舵淳鐢熺被浣滀负浠g悊绫诲瀷
+ public class DriverDispatchProxy : DispatchProxy
+ {
+ private DriverInstance _instance;
+
+ /// <summary>
+ /// 鍒涘缓瀹炵幇 T 鐨勯┍鍔ㄥ姩鎬佷唬鐞嗭紙T 涓哄涓昏嚜瀹氫箟鐨勯腑瀛愭帴鍙o級銆�
+ /// </summary>
+ public static T Create<T>(DriverInstance instance) where T : class
+ {
+ if (!typeof(T).IsInterface)
+ {
+ throw new ArgumentException(
+ $"T 蹇呴』鏄帴鍙o紙椹卞姩绫诲瀷涓嶅彲璺ㄥ煙灏侀�侊紝瀹夸富涔熸棤娉曞紩鐢ㄥ巶鍟嗙▼搴忛泦锛夛紝瀹為檯涓� {typeof(T)}",
+ nameof(T));
+ }
+
+ // DispatchProxy.Create<T, TProxy> 瑕佹眰 TProxy 涓洪潪娉涘瀷绫伙紝
+ // 鍥犳浠g悊瀹炰緥鐨勫垱寤轰笌鐘舵�佹敞鍏ュ垎涓ゆ瀹屾垚
+ var proxy = (DriverDispatchProxy)(object)Create<T, DriverDispatchProxy>();
+ proxy._instance = instance;
+ return (T)(object)proxy;
+ }
+
+ protected override object Invoke(MethodInfo targetMethod, object[] args)
+ {
+ if (targetMethod == null)
+ {
+ throw new NotSupportedException("鏃犳硶瑙f瀽鎺ュ彛鏂规硶锛堝彲鑳界敱娉涘瀷鏂规硶璋冪敤寮曡捣锛�");
+ }
+
+ var mi = _instance.GetMethods().FirstOrDefault(m =>
+ string.Equals(m.Name, targetMethod.Name, StringComparison.Ordinal)
+ && m.ParameterCount == (args?.Length ?? 0));
+
+ if (mi == null)
+ {
+ throw new MissingMethodException(_instance.TypeFullName, $"{targetMethod.Name}({args?.Length ?? 0} 鍙傛暟)鈥斺�旈┍鍔ㄦ湭鎻愪緵鍚屽悕涓斿弬鏁颁釜鏁颁竴鑷寸殑鏂规硶");
+ }
+
+ var direct = _instance.InvokeDirect(mi.Index, args);
+ if (!direct.Success)
+ {
+ throw new InvalidOperationException($"椹卞姩鏂规硶 {targetMethod.Name} 鎵ц澶辫触: {direct.Error}");
+ }
+
+ // out/ref 鍥炲~锛氭妸椹卞姩鍩熷唴鐨勫洖濉�煎啓鍥� args 鏁扮粍锛�
+ // DispatchProxy 浼氳嚜鍔ㄥ悓姝ュ洖璋冪敤鏂圭殑 out/ref 鍙橀噺
+ if (direct.ArgsOut != null && args != null)
+ {
+ var count = Math.Min(args.Length, direct.ArgsOut.Length);
+ for (var i = 0; i < count; i++)
+ {
+ args[i] = direct.ArgsOut[i];
+ }
+ }
+
+ return direct.Value;
+ }
+ }
+}
--
Gitblit v1.9.1