0

Is there any alternative way to perform the operation:

textWriter.Write(myBigObject.ToString())

such that:

  • myBigObject is 'streamed' into the text representation without creating the whole string object in memory
  • there are no additional classes or objects used, beside myBigObject and textWriter

Example: Imagine that myBigObject has 50 string fields. There is no point in joining all these fields in a big string and then writing the object to a file, if it is somehow possible to write the strings one by one to the file.

Andrei Bozantan
  • 3,781
  • 2
  • 30
  • 40
  • Are you trying to write out a string, or a binary representation of the object? Because you can't have a string without creating space for it in memory unless you do something fancy. – Codeman Sep 03 '14 at 20:09
  • 3
    `ToString` entirely encapsulates its way of creating a string, and there is no `object` method that streams a string representation. Without more details, that's a "no". – zneak Sep 03 '14 at 20:10
  • “there are no additional classes or objects used” and “`myBigObject` has 50 string fields” contradict each other. Which is it? – Dour High Arch Sep 03 '14 at 21:23

2 Answers2

2

If you have access to the code, you can add a method to MyBigObject that takes a TextWriter and writes out each property. For example:

public class MyBigObject
{
    public void Write(TextWriter writer)
    {
        writer.Write(bigStringField1);
        writer.Write(bigStringField2);
        // etc.
    }
}

If sub-classes of MyBigObject need to write their own representation, then make the method virtual, and the sub-classes call the implementation in the base class.

If you don't own the code, and the fields are exposed through properties, you could build an adapter class that takes a MyBigObject and writes out each property. You could also build some extension methods that do the same thing.

If you cannot access the source code, you could use reflection to do examine the fields on the object, grab the value of each field, and Write() out each value's ToString() representation. However, reflection is slower than direct field access, and it involves a lot more intermediate objects. I don't know if using reflection would be worth it in your case.

Paul Williams
  • 16,585
  • 5
  • 47
  • 82
  • This sounds fine, but I still have a problem with it. If I do this my code will look like: `writer.WriteLine("here is my big object:"); myBigObject.Write(writer);`. As you can see the API will be reversed, and this doesn't look good. Is there some workaround for this also? – Andrei Bozantan Sep 03 '14 at 21:48
0

Given the limitations you have outlined this isn't possible. You would have to come up with a way to read the data from your object and write it out on char/byte/line at a time.

If you want to be able to loop over your properties and write them out one at a time then this would be possible using reflection. However I suspect going this route would result in using more memory than your original solution as well as being much more complicated than a simple call to .ToString().

Community
  • 1
  • 1
Horn
  • 388
  • 1
  • 9