我可能做一些愚蠢的事情,但在這裏。反思和私人代表領域的事件在c#
我想通過反射從公共事件中獲取FieldInfo。
檢查此功能:
public void PlotAllFields(Type type) {
BindingFlags all = BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public;
FieldInfo[] fields = type.GetFields(all);
Console.WriteLine(type + "-------------------------");
foreach (var fieldInfo in fields) {
Console.WriteLine(fieldInfo.Name);
}
}
public class Bar : Foo {}
public class Foo {
public string Test;
public event EventHandler Event;
public event RoutedEventHandler RoutedEvent;
}
呼叫PlotAllFields(typeof運算(富));返回:
- 測試
- 事件
- RoutedEvent
呼叫PlotAllFields(typeof運算(酒吧));返回:
- 測試
據我所知,事件背後的代表是私有字段 ,所以我不能訪問他們的子類。到現在爲止還挺好。
然後我試過:PlotAllFields(typeof(FrameworkElement)); //從WPF
- _themeStyleCache
- _styleCache
- _templatedParent
- _templateChild
- _flags
- _flags2
- _parent
- _inheritableProperties
- MeasureRequest
- ArrangeRequest
- sizeChangedInfo
- _parentIndex
- _parent
- _proxy
- _contextStorage
嗯......哪裏FrameworkElement的類的14個事件???
那是我的懷疑,要檢查,但毆打:) – flq 2010-09-01 12:51:09
最好的, 3分鐘的答案。我愛Stackoverflow :) – andrecarlucci 2010-09-01 13:06:01