© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•3y ago•
17 replies
berend

❔ getting all methods with an attribute in an optimized way

hey all, im trying to find a way to add all methods with an attribute to dictionary. i want to be able by entering a string in a console window for debugging purposes in unity.

example of a methods im trying to find:
[Command]
    public void MyCommandMethod()
    {
        // do something!
    }
[Command]
    public void MyCommandMethod()
    {
        // do something!
    }


this is my current way of finding all methods with an attribute:
Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
            foreach (Assembly assembly in assemblies) 
            {
                foreach (Type type in assembly.GetTypes())
                {
                    foreach (MethodInfo method in type.GetMethods(
                        BindingFlags.Public |
                        BindingFlags.NonPublic |
                        BindingFlags.Instance |
                        BindingFlags.Static))
                    {
                        CommandAttribute command = method.GetCustomAttribute<CommandAttribute>();
                        if (command != null)
                        {
                            string key = command.Name == string.Empty ? method.Name : command.Name;
                            if (!_availableCommands.ContainsKey(key))
                            {
                                _availableCommands.Add(key, method);
                            }
                            else
                            {
                                WriteLine($"command with name '{key}' has already been found!", Color.red);
                            }                            
                        }
                    }
                }
            }
Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
            foreach (Assembly assembly in assemblies) 
            {
                foreach (Type type in assembly.GetTypes())
                {
                    foreach (MethodInfo method in type.GetMethods(
                        BindingFlags.Public |
                        BindingFlags.NonPublic |
                        BindingFlags.Instance |
                        BindingFlags.Static))
                    {
                        CommandAttribute command = method.GetCustomAttribute<CommandAttribute>();
                        if (command != null)
                        {
                            string key = command.Name == string.Empty ? method.Name : command.Name;
                            if (!_availableCommands.ContainsKey(key))
                            {
                                _availableCommands.Add(key, method);
                            }
                            else
                            {
                                WriteLine($"command with name '{key}' has already been found!", Color.red);
                            }                            
                        }
                    }
                }
            }


it works, but is obviously not very performant (takes multiple seconds to run this method). although i have seen other unity packages that dont seem to have any performance impact, so im sure its possible do it some other way. any help would be appreciatedOk
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources
Was this page helpful?

Similar Threads

Recent Announcements

Similar Threads

Find all methods with an attribute using GeneratorExecutionContext
C#CC# / help
4y ago
❔ How can i remove methods or classes with an Attribute from compiling?
C#CC# / help
3y ago
Getting all types that implement a custom attribute
C#CC# / help
2y ago
Identifying something with an attribute with IIncrementalSourceGenerator
C#CC# / help
2y ago