I'm working on a project where methods are directly connected to equivalent strings.
Like ListConnections()
is connected to listconnections
.
The issue I'm having is - is there a more efficient way to get methods with this
attribute instance rather than looping through all methods, then looping through their custom attributes and finally comparing unique properties of the attribute to get the method?
Something like this:
[AttributeUsage(AttributeTargets.Method)]
public class MethodStrAttribute : Attribute
{
public string Command { get; set; }
public MethodInfo Method
{
get
{
//get method attached to `this` attribute instance
}
}
}
Because in such case, I wouldn't have to loop through all methods but instead get all attributes, then do Where(x => x.Command == cmd).SingleOrDefault()?.Method.Invoke()
edit
Example usage of the attribute:
[MethodStr("listconnections")]
public static void ListConnections()
{
}