using System.Collections.Concurrent; using System.Reflection; using AyCode.Services.SignalRs; namespace AyCode.Models.Server.DynamicMethods; public class AcDynamicMethodCallModel where TAttribute : TagAttribute { public object InstanceObject { get; init; } public ConcurrentDictionary> MethodsByMessageTag { get; init; } = new(); public AcDynamicMethodCallModel(Type instanceObjectType) : this(instanceObjectType, null!) { } public AcDynamicMethodCallModel(Type instanceObjectType, params object[] constructorParams) : this(Activator.CreateInstance(instanceObjectType, constructorParams)!) { } public AcDynamicMethodCallModel(object instanceObject) { InstanceObject = instanceObject; foreach (var methodInfo in instanceObject.GetType().GetMethods()) { if (methodInfo.GetCustomAttribute(typeof(TAttribute)) is not TAttribute attribute) continue; if (MethodsByMessageTag.ContainsKey(attribute.MessageTag)) throw new Exception($"Multiple SignaRMessageTag! messageTag: {attribute.MessageTag}; methodName: {methodInfo.Name}"); MethodsByMessageTag[attribute.MessageTag] = new AcMethodInfoModel(attribute, methodInfo!); } } }