为什么我收到无法序列化的错误类型 'mamlaka_lab.BL.SendClass'?



我需要使用C#制作SMS程序,我从短信公司获得了示例代码,并将其粘贴到我的项目中但当点击SEND按钮并得到这个错误时,它没有发送:

Type 'mamlaka_lab.BL.SendClass' cannot be serialized. Consider marking it with the DataContractAttribute attribute, and marking all of its members you want serialized with the DataMemberAttribute attribute.  If the type is a collection, consider marking it with the CollectionDataContractAttribute.  See the Microsoft .NET Framework documentation for other supported types.

这是完整的代码:

1-发送类别:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.Serialization;
namespace mamlaka_lab.BL
{
class SendClass
{
private string _recipients = string.Empty;
private string _body = string.Empty;
private string _sender = string.Empty;
[DataMember()]
public string recipients
{
set
{
_recipients = value;
}
get
{
return _recipients;
}
}
[DataMember()]
public string body
{
set
{
_body = value;
}
get
{
return _body;
}
}
[DataMember()]
public string sender
{
set
{
_sender = value;
}
get
{
return _sender;
}
}
}
}

2-JsonSerialize代码:

private string JSONSerialize(mamlaka_lab.BL.SendClass objStudent)
{
MemoryStream stream = new MemoryStream();
DataContractJsonSerializer jsonSer = new DataContractJsonSerializer(typeof(mamlaka_lab.BL.SendClass));
jsonSer.WriteObject(stream, objStudent);
stream.Position = 0;
StreamReader sr = new StreamReader(stream);
return sr.ReadToEnd();
}

3-SMSEND_Click代码:

private void SMSSEND_Click(object sender, EventArgs e)
{
string strResponse;
try
{
ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls;
ServicePointManager.Expect100Continue = true;
HttpWebRequest req = (HttpWebRequest)WebRequest.Create("https://api.taqnyat.sa/v1/messages");
req.Method = "POST";
req.ContentType = "application/json";
req.Headers.Add("Authorization", "Bearer " + TxtBearer.Text);

mamlaka_lab.BL.SendClass objStudent = new mamlaka_lab.BL.SendClass();
//  objStudent.body = smsBody;
//  objStudent.recipients = txtMobile.Text;
//  objStudent.sender = sender.ToString();
objStudent.body = TxtBody.Text;
objStudent.recipients = TxtRecipients.Text;
objStudent.sender = TxtSender.Text;

Byte[] byteArray = System.Text.Encoding.UTF8.GetBytes(JSONSerialize(objStudent));
req.ContentLength = byteArray.Length;
Stream newStream = req.GetRequestStream();
newStream.Write(byteArray, 0, byteArray.Length);
newStream.Close();

StreamReader stIn = new StreamReader(req.GetResponse().GetResponseStream());
strResponse = stIn.ReadToEnd();
stIn.Close();

TxtResult.Text = strResponse;
}
catch (WebException ex)
{
using (WebResponse response = ex.Response)
{
HttpWebResponse httpResponse1 = (HttpWebResponse)response;
try
{
using (Stream data = response.GetResponseStream())
{
using (var reader = new StreamReader(data))
{
strResponse = reader.ReadToEnd();
}
}
TxtBalance.Text = strResponse;
}
catch (Exception exGetResp)
{
throw exGetResp;
}
}
}
catch (Exception ex)
{
throw ex;
}
finally
{
}
}

当我运行SMS提供商发送消息时提供的应用程序示例代码时但是当我把代码复制到我的项目中时,它没有发送并显示这个错误,

我逐行检查了整个代码,但它不是从我的项目发送的

我需要你的帮助,请如何解决这个错误

当我运行应用程序时,我会写承载代码、发送者、接收者和短信文本,然后单击发送按钮

在此链接中,来自SMS提供商的示例代码

在此处输入链接描述

我认为您可以尝试将[DataContract]添加到SendClass中。https://learn.microsoft.com/en-us/dotnet/api/system.runtime.serialization.datacontractserializer?view=net-6.0#示例

非常感谢我的朋友将[DataContract]添加到SendClass解决了问题,并发送了短信。

这是最后一次SendClass:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.Serialization;
namespace mamlaka_lab.BL
{
[DataContract]
class SendClass
{
private string _recipients = string.Empty;
private string _body = string.Empty;
private string _sender = string.Empty;

[DataMember()]
public string recipients
{
set { _recipients = value; } 
get { return _recipients;  }
}
[DataMember()]
public string body
{
set { _body = value; }
get {  return _body; }
}
[DataMember()]
public string sender
{
set { _sender = value; }
get { return _sender;  }
}
}
}

相关内容

  • 没有找到相关文章

最新更新