所以我创建了这个函数来获取所有带有custom属性的方法 ExecuteFromConsole
[ExecuteFromConsole("test", "help")]
static void Test(string[] args)
{
Debug.Log("Test Ran");
}
public void AddAttributeCommands()
{
//get all methods with the execute attribute
var methods = AppDomain.CurrentDomain.GetAssemblies()
.SelectMany(x => x.GetTypes())
.Where(x => x.IsClass)
.SelectMany(x => x.GetMethods())
.Where(x => x.GetCustomAttributes(typeof(ExecuteFromConsoleAttribute), false).FirstOrDefault() != null);
//adds them to commands
foreach (var method in methods)
{
Debug.Log("Found attribute");
ExecuteFromConsoleAttribute attribute = (ExecuteFromConsoleAttribute)method.GetCustomAttributes(typeof(ExecuteFromConsoleAttribute), false).First();
if(!method.IsStatic)
{
Debug.LogError("ExecuteFromConsoleAttribute must be used on static functions only");
continue;
}
CommandFunc func = (CommandFunc)Delegate.CreateDelegate(typeof(CommandFunc), method);
AddCommand(attribute.command, func, attribute.help);
}
}
当我最初对其进行测试时,它工作得很好,但是现在它将永远不会进入foreach循环,Debug.log("found attribute")
这意味着它找不到带有属性的明显在其正上方的方法。AFAIK我没有修改任何应该影响到此的东西。
是否有人对为什么它不起作用或我是否正在做错所有事情有真知灼见,而我应该有更好的方法来做呢?
如果影响到任何事情,该项目就是统一的
默认GetMethods
值为“任何公开”,但您的方法为非方法。尝试添加BindingFlags.NonPublic | BindingFlags.Static
到GetMethods
呼叫中,以给予更多提示。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句