Borrowing from Matt's reflection code, here are some extension methods that get the job done:
using System.IO;
using System.Reflection;
/// <summary>
/// Contains extension methods for this namespace.
/// </summary>
public static class ExtensionMethods
{
/// <summary>
/// Gets the current read position of the StreamReader.
/// </summary>
/// <param name="streamReader">The StreamReader object to get the position for.</param>
/// <returns>Current read position in the StreamReader.</returns>
public static int GetPosition(this StreamReader streamReader)
{
// Based on code shared on www.daniweb.com by user mfm24(Matt).
int charpos = (int) streamReader.GetType().InvokeMember(
"charPos",
BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.GetField,
null,
streamReader,
null);
int charlen= (int) streamReader.GetType().InvokeMember(
"charLen",
BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.GetField,
null,
streamReader,
null);
return (int)streamReader.BaseStream.Position - charlen + charpos;
}
/// <summary>
/// Sets the current read position of the StreamReader.
/// </summary>
/// <param name="streamReader">The StreamReader object to get the position for.</param>
/// <param name="position">The position to move to in the file, starting from the beginning.</param>
public static void SetPosition(this StreamReader streamReader, long position)
{
streamReader.BaseStream.Seek(position, SeekOrigin.Begin);
streamReader.DiscardBufferedData();
}
}
Unfortunately, when I try to use this code, it all builds but raises an exception at run-time:
System.FieldAccessException was unhandled
Message=System.IO.StreamReader.charPos
StackTrace:
at System.RuntimeType.InternalInvokeMember(Type thisType, String name, BindingFlags invokeAttr, Binder binder, Object target, Object[] args, ParameterModifier[] modifiers, CultureInfo culture, String[] namedParameters, StackCrawlMark& stackMark)
at System.Type.InvokeMember(String name, BindingFlags invokeAttr, Binder binder, Object target, Object[] args)
at ExtensionMethods.GetPosition(StreamReader streamReader)
Is there something I've done wrong to call it, or does it mean that in my environment (Windows Phone development), this method won't work?
Regards
Philip