通过反射访问TextSegment的值

时间:2013-03-15 14:01:45

标签: c# wpf reflection

我在Object类型的对象中有一个'System.Windows.Documents.TextSegment'类型的对象。 TextSegment-Struct我不能在我的代码中使用,因为它是.net-framework的内部代码。

我想要做的是,访问TextSegment类型对象中的Start-和End-Property。我通过反射使用以下代码尝试了它:

// 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的值?

1 个答案:

答案 0 :(得分:2)

该代码响起a bell;)...

你有一个拼写错误(BindingFlags末尾缺少括号)和BindingFlags,你只需要Instance和NonPublic,但我找不到真正的问题。

也许您需要提供更多代码,因为我已经检查过,这对我来说很好用:

enter image description here

您是否检查过值IS实际上不为空?

enter image description here