lundi 16 octobre 2017

Reflection Help Needed: GetProperty Returns Unexpected Type

I am working with the .NET PropertyGrid object. At runtime, I need to inspect the Label property for the PropertyGrid's PropertyGridView.SelectedGridEntry object.

The first problem is that SelectedGridEntry is not a public property--it appears to be internal. So I needed to resort to reflection. I believe I successfully obtained a PropertyGridView instance object using this simple line of code:

var gridView = PropertyGrid.Controls[2];

(Let's accept for the dubious assumption that the PropertyGridView object always is available at PropertyGrid.Controls1.)

Reflection aside for the moment, I used the Visual Studio 2017 debugger at runtime to inspect the PropertyGrid's PropertyGridView object. Here is the image of that:

enter image description here

This image shows that the gridView object has a property called SelectedGridEntry. Notice its type is System.Windows.Forms.PropertyGridInternal.PropertyDescriptorGridEntry. Unfortunately, I cannot cast to the PropertyDescriptorGridEntry type because I cannot get access to the System.Windows.Forms.PropertyGridInternal namespace (Microsoft doesn't make that available, it seems).

In an effort to obtain the SelectedGridEntry object, I used this line of code:

var selectedGridEntry = PropertyGrid.Controls[2].GetType().GetProperty("SelectedGridEntry", BindingFlags.Instance | BindingFlags.NonPublic);

Unfortunately, this returns a var of type GridEntry:

enter image description here

I don't understand why the type obtained through reflection isn't the expected PropertyDescriptorGridEntry type. And since I can't seem to gain access to the namespace I need, I can't cast the GridEntry object to a PropertyDescriptorGridEntry object.

How can I assign a local variable to the SelectedGridEntry object and ensure it's the correct PropertyDescriptorGridEntry type? I feel if I succeed in doing that, I then can obtain that object's Label property value...





Aucun commentaire:

Enregistrer un commentaire