vendredi 20 mars 2020

How to get private dictionary from instance using reflection [duplicate]

Say there is a class definition in a 3rd party library, that I cannot modify. E.g.

public class MyDataClass
{
    private Dictionary<string, int> InternalData { get; set; }

    // Public methods to modify InternalData
}

If I debug an application that has an instance of this class then I can see the private members of the instance, when I hover over the code and expand out the inspection dialog. This might be okay when I have a small Dictionary but if I have a large dictionary and I want to output it to a file or in the output window to debug something, then how can I do this?

I know reflection can enable me access to see private members but how is this achieved?

So if I wanted to use reflection to assign the internal Dictionary to my variable internalData below, then how is this done in this method example?

public void GetReflectedData()
{
    MyDataClass myData = new MyDataClass();

    // Call some methods to add data.

    // Reflection calls here

    Dictionary<string, int> internalData;
}

Thanks





Aucun commentaire:

Enregistrer un commentaire