C
C#12mo ago
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 Ok
10 Replies
Sossenbinder
Sossenbinder12mo ago
Your current code would also examine assemblies of .Net itself I assume you can filter these out?
mtreit
mtreit12mo ago
If you're using reflection your perf is already going to be out the window
phaseshift
phaseshift12mo ago
Aside from filtering out assemblies starting with System etc, thats about the size of it
mtreit
mtreit12mo ago
Yeah, making sure you are only looking at relevant assemblies is probably going to make that much faster. Also, don't do this:
if (!_availableCommands.ContainsKey(key))
{
_availableCommands.Add(key, method);
}
if (!_availableCommands.ContainsKey(key))
{
_availableCommands.Add(key, method);
}
Use TryAdd instead
berend
berend12mo ago
yeah, but I dont really see any other way, but I may be missing something forgot about that one, thanks🙂
mtreit
mtreit12mo ago
Do you know how many assemblies you need to process? If you want to speed this up you can use a concurrent dictionary and process each assembly in parallel.
berend
berend12mo ago
I think so yes
mtreit
mtreit12mo ago
Just throw a Parallel.ForEach on there and use a ConcurrentDictionary, skip assemblies you know you don't need to process and you should be good. ez
berend
berend12mo ago
ah cool, didnt realize it would be that simple thanks!
Accord
Accord12mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server
More Posts
❔ anyone interested in helping a starter?Im a starter looking to start learning c#. after spending days researching where to start im still k❔ How to mock HttpClient that is automatically applied in class without IHttpClientFactoryHello, I have a problem because I don't know how can I mock ``HttpClient`` in my tests for ``StripeC❔ Textmate themes for custom languages?So it's apparently possible to add support for custom syntax highlighting via textmate grammars acco❔ My Label Doesn't show up even though no errors.Hi, I am making like PC Stats app like HWInfo or something like that, but I am using Open Hardware M❔ NuGet broken, i have no ideaSo it has been ages since i coded in c#, turns out my NuGet is broken or something❔ C++ Program displaying same output regardless of input value.Attached is my code and the output I receive with inputs "5000", "20000", and "34567" The program is❔ Learning ArchitecturesHi! Can you recommend any books, articles, links to github that will help me better understand all k❔ (Due in about two days) Fish Project for C# UnityHiya! I'm new to C# and im currently in university for Game Development in two days I have this proj❔ Are strings the best way for Server-Client UDP communication?Hi I'm developing a server-client udp communication system using System.Net ( with classes like UdpC✅ [DDD] Reference IdentityUser in Domain model classHi, I'm starting my jurney with DDD so please take it easy on me 😄 Any pointers on how to referenc