0

I'm using a NuGet Package called DevExpress.Xpo and its DataStorePool class has a private int called connections. I need to somehow use its value in another class, but the DataStorePool class is locked as "metadata", so I can't set the int to public nor create a method that returns it. What can I do to obtain the value of the int?

1 Answers1

0

You could create an extension method to do it. It can look like that:

Foo foo = new Foo();
string c = foo.GetFieldValue<string>("_bar");

And the way to create it:

public static class ReflectionExtensions {
    public static T GetFieldValue<T>(this object obj, string name) {
        // Set the flags so that private and public fields from instances will be found
        var bindingFlags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance;
        var field = obj.GetType().GetField(name, bindingFlags);
        return (T)field?.GetValue(obj);
    }
}

OR

You can use BindingFlags.NonPublic and BindingFlags.Instance flags

FieldInfo[] fields = myType.GetFields(
                         BindingFlags.NonPublic | 
                         BindingFlags.Instance);

OR

You could read how to get it with using reflection here

Liad
  • 336
  • 5
  • 15