The StringReader keeps track of its position within the string with a private int
field called _pos
, if you want to reset it you can use a simple extension method like this:
public static void Reset(this StringReader reader)
{
reader.GetType()
.GetField("_pos", BindingFlags.NonPublic | BindingFlags.Instance)
.SetValue(reader, 0);
}
And a test method:
[Test]
public void Reset()
{
const string random = "this is a test string";
using(var reader = new StringReader(random))
{
Assert.AreEqual(random, reader.ReadToEnd());
Assert.IsEmpty(reader.ReadToEnd());
reader.Reset();
Assert.AreEqual(random, reader.ReadToEnd());
Assert.IsEmpty(reader.ReadToEnd());
}
}