简体   繁体   English

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

[英]Convert return type to array C#

I have a method that ts returning Array. 我有一个返回数组的方法。 I have problem with one class that cant convert the return type to array. 我有一个类无法将返回类型转换为数组的问题。

This is my method: 这是我的方法:

      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;
    }

My problem is in else part, not sure how I can convert Model.ReturnResponse() to array now I am getting this error: 我的问题是在其他部分,不确定现在如何将Model.ReturnResponse()转换为数组,但出现此错误:

cannot implicitly convert type ReturnResponse to Model.BaseType[] 无法将类型ReturnResponse隐式转换为Model.BaseType []

in case you like to see my 3 classes: 如果您想看我的3门课:

This is Base class: 这是基类:

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

public class BaseType 
{

}

this is Payment_Gateway class: 这是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; }

and this is ReturnResponse class: 这是ReturnResponse类:

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

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


}

Instead of trying to return a single ReturnResponse : 而不是尝试返回单个ReturnResponse

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

Return an array, with the single element in it, since that's what your method is expected to return: 返回一个数组,其中包含单个元素,因为这是您的方法期望返回的结果:

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

Instead of the hierarchy you have designed here, it would be easier if you encapsulate your return data in a response object that indicate a response type (success or failure) and the resulting data. 如果将返回数据封装在指示响应类型(成功或失败)和结果数据的响应对象中,则将比您在此处设计的层次结构更容易。

that class could look like the following 该类可能如下所示

public enum ResponseTypes
{
    Success,
    Failure
}

public class Response
{
    public ResponseTypes ResponseType {get; set;}
    public Exception Error {get;set;}
    public BaseType[] Data {get;set;}
}

this way it works like an envelope to your actual data and provides state to the caller to know if the call succeeded or not, and maybe for what reason, before reading the data (result) 这样,它就像实际数据的信封一样,并为调用者提供状态,以便在读取数据之前(结果)知道调用是否成功(可能是由于什么原因)

its the same way WCF acts under the hood (in SOAP, at least) that it surrounds your data contract with a message envelope/enclosure to add more metadata for the caller to interpret the call status. 与WCF在后台(至少在SOAP中)起作用的方式相同,即它用消息信封/外壳将数据协定包围,从而为调用者添加更多元数据以解释呼叫状态。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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