Version: 2022.3

TypeCache.GetMethodsWithAttribute

切换到手册
public static TypeCache.MethodCollection GetMethodsWithAttribute ();
public static TypeCache.MethodCollection GetMethodsWithAttribute (Type attrType);

参数

attrType 属性类型。

返回

MethodCollection 返回用 **T** 属性标记的方法的 MethodInfo 集合。

描述

获取用 **T** 属性标记的方法的集合。

通过此方法可以快速访问从 Unity 域程序集加载并用特定属性标记的所有方法。结果中还包含用指定属性的上级标记的方法。

using UnityEditor;
using System.Collections.Generic;

public class Example { static void ScanInitializeOnLoadMethods() { var extractedMethods = TypeCache.GetMethodsWithAttribute<InitializeOnLoadMethodAttribute>(); foreach (var m in extractedMethods) { if (m.IsPrivate) continue; //... }

for (int i = 0; i < extractedMethods.Count; ++i) { if (extractedMethods[i].IsPublic) continue; //... } } }

**注意:**返回的 MethodCollection 为只读,具有线程安全性。