我正在尝试根据枚举器的实例收集应用于 IEnumerator 方法的属性。例如:
static class Program {
[SomeAttribute("Hello")]
static IEnumerator Test() {
yield return 1;
yield return "x";
}
static void Main() {
var foo = Test();
// ... How to get the attribute from the 'foo' instance?
}
}
foo.GetType()返回生成的 type Program.<Test>d__4,所以它似乎对生成它的方法有所了解。如何向后工作以找到 MethodInfo Test?从那里我可以得到属性。
Program我还尝试在每个方法的属性中搜索类型,MethodInfo.ReturnType以找到返回的类型Program.<Test>d__4。令我惊讶的是,我想要的 MethodInfo 只是表示System.Collections.IEnumerator返回类型。
也许对内部有更好了解的人可以解释我如何从生成的类型中获取 MethodInfo,反之亦然。
谢谢。