Looking at the MSDN docs on Encoder / Decoder, I see a few comments like "Notes 
to inheritors" and "Notes to implementers", so it seems to me that the door has 
Microsoft has left the door for inheritance open on purpose.  That said, I'd 
agree that it has been implemented in a slightly odd way.  I believe the 
logical separation between Encoding and Encoder / Decoder is that the latter 
are allowed to (or supposed to) have state regarding what is being encoded / 
decoded, where the actual Encoding instance does not?

I think your solution to override the GetDecoder is going to work, and probably 
how it's meant to be done, even if it's a bit more of an effort.

-- Daniel.

-----Original Message-----
From: Discussion of advanced .NET topics. [mailto:[EMAIL PROTECTED] On Behalf 
Of Sébastien Lorion
Sent: 18 September 2007 09:29 AM
To: [email protected]
Subject: [ADVANCED-DOTNET] Creating/using a custom Encoding class

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

===================================
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