假设我有以下代表:
public delegate void Example();
和一个类,如下所示:
public class TestClass {
Example FailingTest = () => Assert.Equal(0,1);
}
如何使用反射来获得名称“FailingTest”?
到目前为止,我已经尝试过:
var possibleFields = typeof(TestClass).GetFields(relevant_binding_flags)
.Where(x => x.FieldType.Equals(typeof(Example)));
foreach(FieldInfo oneField in possibleFields) {
// HERE I am able to access the declaring type name
var className = oneField.ReflectedType.Name; // == "TestClass"
// but I am not able to access the field
// name "FailingTest" because:
var fieldName = oneField.Name; // == "CS$<>9__CachedAnonymousMethodDelegate1"
}
在调试器中单步执行,我找不到声明字段名称“FailingTest”的路径。
该信息是在运行时保留还是在分配匿名委托时丢失?