3

I have an object of type 'System.Windows.Documents.TextSegment' in an object of type Object. The TextSegment-Struct i can not use in my code, because it's internal code of the .net-framework.

What I want to do is, accessing the Start- and End-Property in the object of type TextSegment. I tried it by reflection with the following code:

// 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;

Now I don't know how to access the value of the FieldInfo.

I tried it with the following codes:

object value1 = info.GetValue(segments[0]);
object value2 = info.GetValue(null);

but nothing worked.

How can i get the value of the TextSegment?

Tomtom
  • 9,087
  • 7
  • 52
  • 95
  • can you try something like the following `var value1 = segments.GetType().GetProperties().FirstOrDefault( p => p.Name == yourPropertyName);` – MethodMan Mar 15 '13 at 14:22
  • no, this doesn't work. I get null for value1 – Tomtom Mar 15 '13 at 14:27
  • I just realized that you are wanting FieldInfo look at this Stackoverflow posting I was thinking about PropertyInfo http://stackoverflow.com/questions/5090224/reflection-get-type-of-fieldinfo-object – MethodMan Mar 15 '13 at 14:28

1 Answers1

2

That code rings a bell ;)...

You have a typo (missing bracket at the end of the BindingFlags), and of the BindingFlags, you would only need Instance and NonPublic, but I can't find a real issue.

Perhaps you need to provide more code, because I have checked and this works fine for me:

enter image description here

Have you checked that the value IS in fact not null?

enter image description here

Community
  • 1
  • 1
Mike Fuchs
  • 12,081
  • 6
  • 58
  • 71
  • I see exact the same as you. But how do i store this? I can't create an object of TextPointer or something else. How do I get the value of Start and End into a property. That's the problem... :( – Tomtom Mar 23 '13 at 10:12
  • I don't quite understand. If value1 shows up in the debugger like that, you already have it, no? Store it in a property of type `object`. Perhaps you need to tell me what you want to do with it after. – Mike Fuchs Mar 23 '13 at 12:23
  • Yes, of course I'm able to store the Start-Property into a property of type object. But I need to access the concret value of Start. This is the problem. If I store it into an object I'm not able to access properties. – Tomtom Mar 24 '13 at 21:41
  • DocumentSequenceTextPointer is an internal sealed class, you will not be able to store in in a type that will allow you to easily access its properties. If you need those properties, you must resort to reflection again: `value1.GetType().GetProperty/GetField...` – Mike Fuchs Mar 25 '13 at 10:38