C
C#•10mo ago
goated

how to use attribute to get functions and store in dict?

i want to store all functions with attribute from a specific instance of class(verifyHandler) to the dict i tried to code with the help of gpt and im having some troubles idk much about reflection so idk how to fix public void VerifyMethods() { System.Type targetType = typeof(verifyHandler); MethodInfo[] methods = targetType.GetMethods(BindingFlags.Instance | BindingFlags.Public); foreach (MethodInfo method in methods) { verifyAttribute verifyAttribute = (verifyAttribute)System.Attribute.GetCustomAttribute(method, typeof(verifyAttribute)); if (verifyAttribute != null) { System.Func<JoinRequestMessage, bool> func = (System.Func<JoinRequestMessage, bool>)System.Delegate.CreateDelegate(typeof(System.Func<JoinRequestMessage, bool>),method,verifyhandler); JoinFailedCallback callback = verifyAttribute.callbackmsg; checkers.Add(func, callback); } } Debug.Log($"checkers(count):{checkers.Count}"); }
2 Replies
Buddy
Buddy•10mo ago
Use source generation, reflection is a no-no in Unity. Reflection leaves permanent overhead
goated
goatedOP•10mo ago
Now I got to learn both reflection and unityeditor 💀
Want results from more Discord servers?
Add your server