Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to set DecoderFallback property of an Encoding type

I'm attempting to set the DecoderFallback property of an arbitrary (but supported) encoding in my C# app. Essentially what i'm trying to do is this:

ASCIIEncoding ascii = new ASCIIEncoding();
ascii.DecoderFallback = new DecoderExceptionFallback();

I'm getting an exception i've never seen before:

System.InvalidOperationException was unhandled Message="Instance is read-only." Source="mscorlib"
StackTrace: at System.Text.Encoding.set_DecoderFallback(DecoderFallback value) at <... into my app...> InnerException:

I was unable to find any MSDN documenation with examples of how to use that property. If anyone could point me to some maybe suggest what is wrong my usage I'd appreciate it. I need to throw an exception upon failure to decode a byte or bytes and cannot afford to let that go unnoticed.

Thanks, brian

like image 430
Brian Sweeney Avatar asked Jun 04 '09 21:06

Brian Sweeney


1 Answers

This property is read-only. You need to use Encoding.GetEncoding() to create your own encode with your configs. This method receive the encode, the EncoderFallback and the DecoderFallback.

var enc = System.Text.Encoding.GetEncoding("ASCII", EncoderFallback.ExceptionFallback, DecoderFallback.ExceptionFallback);

http://msdn.microsoft.com/pt-br/library/89856k4b.aspx

like image 136
palhares Avatar answered Sep 23 '22 21:09

palhares