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