0

I asked a question on here earlier but I didn't explain correctly so I got the right answers to the wrong question.

I am creating an instance of a class, when I get the class back it returns a number of results which are private in the class I am calling.

I am unable to change this class and make them public for various reasons.

What I need to do is enumerate through and get a value of the Text variable that is held:

public class StringReader
{

    private string LongText = "this is the text i need to return";
    private string Text;

    public StringReader()
    {

        Text = LongText;
    }
}

In the method I am trying to get the value of Text I am calling

 StringReader sReader = new StringReader();
 List<StringReader> readers = new List<StringReader>() { sReader};

Readers has LongText and Text but I am struggling to get the text value back.

Instead it just returns the Type to me.

6
  • 2
    If the fields are private, you can't access them outside the StringReader class.
    – Glorfindel
    Jul 31, 2015 at 9:41
  • BugFinder, this is the task I have been given, I cannot modify the StringReader to make them public. Jul 31, 2015 at 9:45
  • 1
    @SimonPrice then your task is nearly impossible. The second easiest way to do would be to disassemble the StringReader class, make those two fields public and compile it and use it instead of the original dll. If it's in-house code there are no consequences to this aside from maybe a butthurt developer. Jul 31, 2015 at 9:45
  • 1
    @SimonPrice definitely not impossible. See my answer.
    – Roy T.
    Jul 31, 2015 at 9:47
  • 2
    As this is your homework - Im guessing there is also context to this - such as you were learning of extending classes and so on.. what was the context to the question - especially as the code has now changed name since your previous post...
    – BugFinder
    Jul 31, 2015 at 9:48

3 Answers 3

3

You will need to use reflection to access the private field. You can access all fields of a type using the GetField(s) method. You can access their values using the GetValue function

public string GetLongText(StringReader reader)
{
    // Get a reference to the private field
    var field = reader.GetType().GetField("LongText", BindingFlags.NonPublic | 
                         BindingFlags.Instance)

    // Get the value of the field for the instance reader
    return (string)field.GetValue(reader);                 
}
5
  • 3
    Although this is correct I am not sure if OP truly has a good understanding of his own question. Statements like Text = LongText seems to validate this statement. It feels like a XY-problem to me.
    – Stefan
    Jul 31, 2015 at 9:49
  • 1
    +1 I agree the example code given is dubious. But the task: getting the value of a private field from a class that for various reasons can't be changed seems straightforward enough. Though there are definitely some caveats. Like that you should never have to rely on anything else than the public interface of a method (in case the class gets updated). However, in practice sometimes you have to do this. Just wanted to add an answer to show that the answer is not a simple "no its impossible".
    – Roy T.
    Jul 31, 2015 at 9:51
  • There are also other considerations to consider. It's quite bad practice to do this and seems an odd way around it. But further reading on it should be recommend, like this source: msdn.microsoft.com/en-us/library/stfy7tfc.aspx
    – Draken
    Jul 31, 2015 at 9:55
  • i agree with all the comments, and this is not the way that i would typically want to do this, but its something that have been given as a task to do. so thank you very much for your help Jul 31, 2015 at 9:57
  • @SimonPrice Another option, if you're on it, would be to wrap it in an extension method XD
    – Stefan
    Jul 31, 2015 at 10:00
2

Fields declared as private are inaccessible outside the class that defined them. You cannot read their value without either

  1. Changing their visibility
  2. Adding an accessor method/property with public visibility
  3. Using reflection (this is not recommended, there is almost always a better way)

What is the use-case here? What are you trying to achieve? If you define your problem a bit more generally maybe we can give a better solution.

0

Without modifying the class, it's impossible using best OOP practices. They are set to private to mean they can only be accessible inside the class only. You need to speak to the original developer of the class and ask why they cannot make a public getter for the private field that would look like this:

public string getText(){
    return this.Text;
}

This would mean the string can't be modified, but you can at least read it.

1
  • 1
    Definitely not impossible, see my answer.
    – Roy T.
    Jul 31, 2015 at 9:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.