gpt4 book ai didi

c# - 将返回类型转换为数组 C#

转载 作者:行者123 更新时间:2023-11-30 22:01:12 25 4
gpt4 key购买 nike

我有一个 ts 返回数组的方法。我遇到了一个无法将返回类型转换为数组的类的问题。

这是我的方法:

      public Model.BaseType[] returnPayment_Gateway()
{
IncomingWebRequestContext request = WebOperationContext.Current.IncomingRequest;
WebHeaderCollection headers = request.Headers;
var settings = new DataContractJsonSerializerSettings { EmitTypeInformation = EmitTypeInformation.Never };

MemoryStream stream1 = new MemoryStream();

DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(Model.BaseType),settings);

Model.Payment_Gateway[] allRecords = null;


if (headers["ServiceAuthentication"] != null)
{
string ServiceAuthentication = headers["ServiceAuthentication"].ToString();
bool serviceAuth = Service_Authentication(ServiceAuthentication);

DAL.DataManager dal = new DAL.DataManager();

if (serviceAuth == true)
{
allRecords = dal.Get_Payment_Gateway();

}
}

else
{


// Create a new customer to return


return new Model.ReturnResponse() { StatusCode = 201, StatusDescription = "Authentication Fails" };


}

return allRecords;
}

我的问题在其他部分,不确定如何将 Model.ReturnResponse() 转换为数组,现在我收到此错误:

不能将类型 ReturnResponse 隐式转换为 Model.BaseType[]

如果您想看我的 3 个类(class):

这是基类:

[Serializable]
[DataContract]
[KnownType(typeof(Payment_Gateway))]
[KnownType(typeof(ReturnResponse))]

public class BaseType
{

}

这是 Payment_Gateway 类:

[DataContract]
public class Payment_Gateway:BaseType
{
[DataMember]
public int Payment_Gateway_ID { get; set; }

[DataMember]
public string Payment_Gateway_Name { get; set; }

[DataMember]
public string Payment_Gateway_URL { get; set; }

[DataMember]
public string Payment_Gateway_Description { get; set; }

这是 ReturnResponse 类:

[DataContract]
public class ReturnResponse:BaseType
{
[DataMember]
public int StatusCode { get; set; }

[DataMember]
public string StatusDescription { get; set; }


}

最佳答案

而不是尝试返回单个 ReturnResponse:

return new Model.ReturnResponse()
{ StatusCode = 201, StatusDescription = "Authentication Fails" };

返回一个数组,其中包含单个元素,因为这是您的方法应返回的内容:

return new[] {
new Model.ReturnResponse()
{ StatusCode = 201, StatusDescription = "Authentication Fails" }
}

关于c# - 将返回类型转换为数组 C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27954141/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com