Cannot set DecoderFallback property of type Encoding

I am trying to set the DecoderFallback property of an arbitrary (but supported) encoding in my C # application. Essentially, I'm trying to do this:

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

I get an exception that I have never seen before:

Exception System.InvalidOperationException unhandled Message = "Read-only instance." Source = "mscorlib"
Stack Traces : in System.Text.Encoding.set_DecoderFallback (DecoderFallback value) at <... in my application ...> InnerException:

I could not find the MSDN documentation with examples of using this property. If someone can point me to someone, perhaps suggest what is wrong, I will be grateful. I need to throw an exception due to the impossibility of decoding a byte or bytes and cannot afford to leave it unnoticed.

Thanks Brian

+3
source share
2 answers

This property is read-only. You must use Encoding.GetEncoding () to create your own code with your configurations. This method gets the encoding, EncoderFallback and DecoderFallback.

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

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

+3
source

http://www.google.com/codesearch?q=DecoderFallback

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

Source: https://habr.com/ru/post/1709816/


All Articles