2

I'm getting an error when trying to deserialize some data into a class. The error is: "System.RuntimeType is inaccessible due to its protection level. Only public types can be processed."

The error and all the other searches I've done say that something isn't public. I've made everything public (but I don't want to have to do that if I can avoid it), but still can't figure it out.

Here is my original code:

private void RequestTest() { SMDataRequest dataRequest = new SMDataRequest(); SMListInvoicesRequest invoiceRequest = new SMListInvoicesRequest(); SMListInvoicesResponse response = (SMListInvoicesResponse)dataRequest.GetData(invoiceRequest); } public class SMDataRequest { private const string URI = UriHere; public SMResponse GetData(SMRequest request) { //SMResponse response; WebRequest webRequest = WebRequest.Create(URI); webRequest.Method = "POST"; webRequest.ContentType = "text/XML"; string parameters = string.Format(request.GetRequest(), USERNAME, PASSWORD); webRequest.ContentLength = parameters.Length; // Write the request StreamWriter requestWriter = new StreamWriter(webRequest.GetRequestStream(), Encoding.ASCII); requestWriter.Write(parameters); requestWriter.Close(); // Do the request to get the response StreamReader responseReader = new StreamReader(webRequest.GetResponse().GetResponseStream()); string response = responseReader.ReadToEnd(); XmlDocument doc = new XmlDocument(); doc.LoadXml(response); XmlNode node = doc.SelectSingleNode("response"); return request.GetResponse(node.FirstChild.InnerXml); } } public abstract class SMRequest { public abstract string Action { get; } public abstract string Data { get; } public abstract string Row { get; } internal const string XML_ACTION = "<root><action>{0}</action>{1}</root>"; internal const string XML_DATA = "<data>{0}{1}</data>"; internal const string XML_ROW = "<row>{0}{1}</row>"; internal const string XML_AUTH = "<auth_username>{0}</auth_username><auth_password>{1}</auth_password>"; private readonly string _userName; private readonly string _password; protected SMRequest() { //Set username and password } public string GetRequest() { return string.Format(XML_ACTION, Action, GetDataInfo()); } public virtual string GetDataInfo() { return string.Format(XML_DATA, GetAuthInfo(), Data); } protected internal abstract SMResponse GetResponse(string responseData); internal string GetAuthInfo() { return string.Format(XML_AUTH, _userName, _password); } internal object DeserializeObject(string xml, object data) { object result = null; xml = xml.Replace("utf-16", "utf-8"); try { XmlSerializer serializer = new XmlSerializer(data.GetType()); MemoryStream stream = new MemoryStream(System.Text.Encoding.ASCII.GetBytes(xml)); result = serializer.Deserialize(stream); } catch (Exception) { } return result; } } [Serializable] public class SMListInvoicesRequest : SMRequest { public override string Action { get { return "list-invoices"; } } public override string Data { get { return string.Empty; } } public override string Row { get { throw new NotImplementedException(); } } protected internal override SMResponse GetResponse(string responseData) { SMListInvoicesResponse invoiceReponse = new SMListInvoicesResponse(); invoiceReponse = (SMListInvoicesResponse)DeserializeObject(responseData, invoiceReponse.GetType()); return invoiceReponse; } } 

Thank you in advance for the help.

1 Answer 1

2

You are trying to deserialize a Type:

 invoiceReponse = (SMListInvoicesResponse)DeserializeObject(responseData, invoiceReponse.GetType()); 

The second parameter, invoiceReponse.GetType(), is passed to the data parameter and the data parameter is used as follows:

XmlSerializer serializer = new XmlSerializer(data.GetType()); 

I think you should change your call to just send invoiceResponse.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. I've been staring at this too long.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.