1
czw
2025-07-04 58c1a87f19a96a1d62df382a01ed0dfd36e2f84b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
{
  "CustomCodes": [
    {
      "ID": "20250701112200484",
      "Name": "AutoThread",
      "Desc": "",
      "ParentID": "",
      "Type": "VsCode",
      "Level": "Item",
      "Statement": "",
      "EditCode": "using Newtonsoft.Json;\r\nusing System;\r\nusing System.Collections.Generic;\r\nusing System.Collections.Concurrent;\r\nusing System.ComponentModel;\r\nusing System.Linq;\r\nusing System.Reflection;\r\nusing System.Text;\r\nusing System.Threading.Tasks;\r\nusing GZ.Modular.Redis;\r\nusing static System.Windows.Forms.VisualStyles.VisualStyleElement;\r\nusing System.Windows.Interop;\r\nusing static System.Windows.Forms.VisualStyles.VisualStyleElement.TextBox;\r\nusing System.Security.Cryptography;\r\nusing System.Windows.Markup;\r\nusing static System.Runtime.CompilerServices.RuntimeHelpers;\r\nusing ServiceStack.Configuration;\r\nusing ServiceStack;\r\n\r\nnamespace GZ.Projects.HnSx\r\n{\r\n    public partial class AutoThread\r\n    {\r\n\r\n        private static AutoThread _instance;\r\n\r\n        // 私有构造函数防止外部实例化\r\n        private AutoThread() { }\r\n\r\n        public static AutoThread Instance\r\n        {\r\n            get\r\n            {\r\n                if (_instance == null)\r\n                {\r\n                    _instance = new AutoThread();\r\n                }\r\n                return _instance;\r\n            }\r\n        }\r\n\r\n        // 线程安全的委托缓存\r\n        private static readonly ConcurrentDictionary<string, Delegate> _methodCache = new ConcurrentDictionary<string, Delegate>();\r\n\r\n        // 方法执行器\r\n        public static object InvokeMethod(object instance, string methodName, params object[] args)\r\n        {\r\n            var cacheKey = $\"{instance.GetType().FullName}_{methodName}\";\r\n\r\n            if (!_methodCache.TryGetValue(cacheKey, out var methodDelegate))\r\n            {\r\n                // 获取方法信息\r\n                var methodInfo = instance.GetType().GetMethod(\r\n                    methodName,\r\n                    BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);\r\n\r\n                if (methodInfo == null)\r\n                    throw new MissingMethodException($\"Method {methodName} not found\");\r\n\r\n                // 创建委托并缓存\r\n                methodDelegate = Delegate.CreateDelegate(\r\n                    GetDelegateType(methodInfo),\r\n                    instance,\r\n                    methodInfo);\r\n\r\n                _methodCache.TryAdd(cacheKey, methodDelegate);\r\n            }\r\n\r\n            // 执行委托\r\n            return methodDelegate.DynamicInvoke(args);\r\n        }\r\n\r\n        // 根据方法签名生成对应的委托类型\r\n        private static Type GetDelegateType(MethodInfo methodInfo)\r\n        {\r\n            var parameterTypes = methodInfo.GetParameters()\r\n                .Select(p => p.ParameterType)\r\n                .ToList();\r\n\r\n            if (methodInfo.ReturnType == typeof(void))\r\n            {\r\n                return System.Linq.Expressions.Expression.GetActionType(parameterTypes.ToArray());\r\n            }\r\n            else\r\n            {\r\n                parameterTypes.Add(methodInfo.ReturnType);\r\n                return System.Linq.Expressions.Expression.GetFuncType(parameterTypes.ToArray());\r\n            }\r\n        }\r\n\r\n        /// <summary>\r\n        /// 配置初始化。\r\n        /// </summary>\r\n        /// <param name=\"tag\"></param>\r\n        /// <param name=\"action\"></param>\r\n        public void ThreadSettingInit(Tag tag, Action action)\r\n        {\r\n\r\n        }\r\n    }\r\n}",
      "Code": "using Newtonsoft.Json;\r\nusing System;\r\nusing System.Collections.Generic;\r\nusing System.Collections.Concurrent;\r\nusing System.ComponentModel;\r\nusing System.Linq;\r\nusing System.Reflection;\r\nusing System.Text;\r\nusing System.Threading.Tasks;\r\nusing GZ.Modular.Redis;\r\nusing static System.Windows.Forms.VisualStyles.VisualStyleElement;\r\nusing System.Windows.Interop;\r\nusing static System.Windows.Forms.VisualStyles.VisualStyleElement.TextBox;\r\nusing System.Security.Cryptography;\r\nusing System.Windows.Markup;\r\nusing static System.Runtime.CompilerServices.RuntimeHelpers;\r\nusing ServiceStack.Configuration;\r\nusing ServiceStack;\r\n\r\nnamespace GZ.Projects.HnSx\r\n{\r\n    public partial class AutoThread\r\n    {\r\n\r\n        private static AutoThread _instance;\r\n\r\n        // 私有构造函数防止外部实例化\r\n        private AutoThread() { }\r\n\r\n        public static AutoThread Instance\r\n        {\r\n            get\r\n            {\r\n                if (_instance == null)\r\n                {\r\n                    _instance = new AutoThread();\r\n                }\r\n                return _instance;\r\n            }\r\n        }\r\n\r\n        // 线程安全的委托缓存\r\n        private static readonly ConcurrentDictionary<string, Delegate> _methodCache = new ConcurrentDictionary<string, Delegate>();\r\n\r\n        // 方法执行器\r\n        public static object InvokeMethod(object instance, string methodName, params object[] args)\r\n        {\r\n            var cacheKey = $\"{instance.GetType().FullName}_{methodName}\";\r\n\r\n            if (!_methodCache.TryGetValue(cacheKey, out var methodDelegate))\r\n            {\r\n                // 获取方法信息\r\n                var methodInfo = instance.GetType().GetMethod(\r\n                    methodName,\r\n                    BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);\r\n\r\n                if (methodInfo == null)\r\n                    throw new MissingMethodException($\"Method {methodName} not found\");\r\n\r\n                // 创建委托并缓存\r\n                methodDelegate = Delegate.CreateDelegate(\r\n                    GetDelegateType(methodInfo),\r\n                    instance,\r\n                    methodInfo);\r\n\r\n                _methodCache.TryAdd(cacheKey, methodDelegate);\r\n            }\r\n\r\n            // 执行委托\r\n            return methodDelegate.DynamicInvoke(args);\r\n        }\r\n\r\n        // 根据方法签名生成对应的委托类型\r\n        private static Type GetDelegateType(MethodInfo methodInfo)\r\n        {\r\n            var parameterTypes = methodInfo.GetParameters()\r\n                .Select(p => p.ParameterType)\r\n                .ToList();\r\n\r\n            if (methodInfo.ReturnType == typeof(void))\r\n            {\r\n                return System.Linq.Expressions.Expression.GetActionType(parameterTypes.ToArray());\r\n            }\r\n            else\r\n            {\r\n                parameterTypes.Add(methodInfo.ReturnType);\r\n                return System.Linq.Expressions.Expression.GetFuncType(parameterTypes.ToArray());\r\n            }\r\n        }\r\n\r\n        /// <summary>\r\n        /// 配置初始化。\r\n        /// </summary>\r\n        /// <param name=\"tag\"></param>\r\n        /// <param name=\"action\"></param>\r\n        public void ThreadSettingInit(Tag tag, Action action)\r\n        {\r\n\r\n        }\r\n    }\r\n}",
      "CusMethod": {
        "Name": "",
        "Params": [],
        "ReturnType": ""
      },
      "CusClass": {
        "Name": "",
        "InheritClass": "",
        "Inherits": []
      }
    }
  ],
  "CreationTime": "2025-07-01 10:23:43",
  "LastWriteTime": "2025-07-04 14:55:05",
  "HasSaved": true
}