简体   繁体   中英

C# Error 'System.Text.Encoding.GetString(byte[])' is inaccessible due to its protection level

I am trying to convert Object to xml using silverlight 5 and c# and i have following error:

Error   1   'System.Text.Encoding.GetString(byte[])' is inaccessible due to its protection level    

corresponding to line:

 return utf8.GetString(mem.ToArray());

in my Xml.cs class

using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Xml.Serialization;
using System.IO;
using System.Text;

namespace SliderLastTry
{
    public static class Xml
    {
        public static string ToXml(this object objectToSerialize)
        {
            var mem = new MemoryStream();
            var ser = new XmlSerializer(objectToSerialize.GetType());
            ser.Serialize(mem, objectToSerialize);
            var utf8 = new UTF8Encoding();
            return utf8.GetString(mem.ToArray());
        }
    }
}

Paramter.cs is:

using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace SliderLastTry
{
    public  class Parameter 
    { 
        public  string Name {get; set; } 
    }  
}

Main function containing class is:

using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace SliderLastTry
{
    public static class ControlClass
    {
        public static void Main()
        {
            Parameter pram = new Parameter();
            pram.ToXml();

        }

    }
}

Could some one please help me in fixing my error ?

I guess silverlight doesn't have the overload which takes only byte[] for some reason.

You just need to use another overload which takes index and count as well.

var bytes = mem.ToArray();
return utf8.GetString(bytes, 0, bytes.Length);

FWIW GetString internally calls another overload the same way :)

To be honest, your best bet here is actually to use StringWriter , since a .NET string is not a direct map to UTF-8 (it is actually UTF-16, if anything):

using(var writer = new StringWriter())
{
    ser.Serialize(writer, objectToSerialize);
    return writer.ToString();
}

This also has the performance advantage of avoiding an additional duplicate of all the data ( string vs byte[] ).

If you must use the byte[] version, you can use a different overload to specify the bounds of the array. Note also that passing in the underlying buffer avoids a third duplicate of the data (ie the underlying byte[] in the memory-stream, the temporary byte[] returned from ToArray() , and the data that ends up in the string ).

return utf8.GetString(mem.GetBuffer(), 0, (int)mem.Length);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM