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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Collections.Concurrent;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using GZ.Modular.Redis;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
using System.Windows.Interop;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.TextBox;
using System.Security.Cryptography;
using System.Windows.Markup;
using static System.Runtime.CompilerServices.RuntimeHelpers;
using ServiceStack.Configuration;
using ServiceStack;
 
namespace GZ.Projects.HnSx
{
    public partial class AutoThread
    {
 
        private static AutoThread _instance;
 
        // 私有构造函数防止外部实例化
        private AutoThread() { }
 
        public static AutoThread Instance
        {
            get
            {
                if (_instance == null)
                {
                    _instance = new AutoThread();
                }
                return _instance;
            }
        }
 
        // 线程安全的委托缓存
        private static readonly ConcurrentDictionary<string, Delegate> _methodCache = new ConcurrentDictionary<string, Delegate>();
 
        // 方法执行器
        public static object InvokeMethod(object instance, string methodName, params object[] args)
        {
            var cacheKey = $"{instance.GetType().FullName}_{methodName}";
 
            if (!_methodCache.TryGetValue(cacheKey, out var methodDelegate))
            {
                // 获取方法信息
                var methodInfo = instance.GetType().GetMethod(
                    methodName,
                    BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
 
                if (methodInfo == null)
                    throw new MissingMethodException($"Method {methodName} not found");
 
                // 创建委托并缓存
                methodDelegate = Delegate.CreateDelegate(
                    GetDelegateType(methodInfo),
                    instance,
                    methodInfo);
 
                _methodCache.TryAdd(cacheKey, methodDelegate);
            }
 
            // 执行委托
            return methodDelegate.DynamicInvoke(args);
        }
 
        // 根据方法签名生成对应的委托类型
        private static Type GetDelegateType(MethodInfo methodInfo)
        {
            var parameterTypes = methodInfo.GetParameters()
                .Select(p => p.ParameterType)
                .ToList();
 
            if (methodInfo.ReturnType == typeof(void))
            {
                return System.Linq.Expressions.Expression.GetActionType(parameterTypes.ToArray());
            }
            else
            {
                parameterTypes.Add(methodInfo.ReturnType);
                return System.Linq.Expressions.Expression.GetFuncType(parameterTypes.ToArray());
            }
        }
 
        /// <summary>
        /// 配置初始化。
        /// </summary>
        /// <param name="tag"></param>
        /// <param name="action"></param>
        public void ThreadSettingInit(Tag tag, Action action)
        {
 
        }
    }
}