这是该类,我想获取其中使用的所有类型的列表:
public class TestClass : MonoBehaviour{
private UIManager _manager;
private void Initialize(UIManager manager){
_manager = manager;
}
}
然后我想通过运行这样的东西:
Assembly assembly = typeof (TestClass).Assembly;
foreach (Type type in assembly.GetTypes()){
Debug.Log(type.FullName);
}
只会给我 UIManager 类型。但它似乎给了我项目中使用的所有类型的列表。
我怎样才能得到这个类中使用的类型?
(如您所见,TestClass 继承自 MonoBehaviour,但我不想要那里使用的类型,我想要 TestClass 中专门使用的类型)。
最佳答案
如果您想知道用于方法/文件/属性的类型 - 使用基本反射来枚举声明中使用的每个方法/字段和抓取类型。
即对于你调用的方法 Type.GetMethods
而不是挖掘 MethodInfo
类似 MethodInfo.ReturnType 的属性获取使用过的类型。可能通过每个基类/接口(interface)递归地向下查找所有依赖项。
如果您想知道方法内部使用了哪些类型,您需要使用某种 IL 解析器,因为反射不提供查看方法主体的方法。 sample 可在 Get types used inside a C# method body 中找到.
请注意,现有工具已经提供了类似的功能 - 即 ReSahrper 具有“根据模块查找代码”和“查找类型/方法/...的用法”。
关于c# - 获取特定类中使用的类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29520792/