我在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