我在Object類型的對象中有一個'System.Windows.Documents.TextSegment'類型的對象。 TextSegment-Struct我不能在我的代碼中使用,因爲它是.net框架的內部代碼。通過反射訪問TextSegment的值
我想要做的是訪問TextSegment類型的對象中的開始和結束屬性。我試着用以下代碼反思:
// This object is of type TextSegment
object textSegment = segments[0];
FieldInfo info = textSegment.GetType().GetField("_start", BindingFlags.IgnoreCase |
BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance| BindingFlags.Static;
現在我不知道如何訪問FieldInfo的值。
我用下面的代碼試了一下:
object value1 = info.GetValue(segments[0]);
object value2 = info.GetValue(null);
,但毫無效果。
如何獲取TextSegment的值?
可以嘗試像下面這樣'VAR值1 = segments.GetType()的GetProperties()FirstOrDefault(P => p.Name == yourPropertyName);' – MethodMan 2013-03-15 14:22:42
沒有,這不工作。我得到null爲value1 – Tomtom 2013-03-15 14:27:28
我只是意識到你想FieldInfo看看這個Stackoverflow發佈我正在考慮PropertyInfo http://stackoverflow.com/questions/5090224/reflection-get-type-of-fieldinfo-object – MethodMan 2013-03-15 14:28:31