Given the following:

class MyEncoding
  : ASCIIEncoding
{
  public override int GetChars(byte[] bytes, int byteIndex, int byteCount,
char[] chars, int charIndex)
  {
    // do custom conversion here
  }
}

static void ReadTest(string path, int bufferSize)
{
  using (StreamReader sr = new StreamReader(path, new FastASCIIEncoding(),
false, bufferSize))
  {
    char[] buffer = new char[BufferSize];
    int count;

    while ((count = sr.Read(buffer, 0, bufferSize)) > 0)
    {
       // do stuff
    }
  }
}

My overriden GetChars method is never called. Internally, the StreamReader
may/may not create a DecoderNLS instance and then call an internal method in
ASCIIEncoding which cannot be overriden:

internal override unsafe int GetChars(byte* bytes, int byteCount, char*
chars, int charCount, DecoderNLS decoder)


Why is it behaving like that ?
That gives the impression that the Encoding classes are not meant to
be derived, yet they are not sealed.


The best solution I see is to override the GetDecoder method in
ASCIIEncoding and return a custom Decoder which will call
my conversion method.

Anyone can comment on that ? Am I doing the right thing ?

-- 
Sébastien
www.sebastienlorion.com

===================================
This list is hosted by DevelopMentor®  http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com

Reply via email to