编程语言
首页 > 编程语言> > C#中自定义返回类型

C#中自定义返回类型

作者:互联网

在项目中会用到AjaxResult的返回值,根据自己的需要进行自定义,如下参考:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace ToolsUse.CommonHelper
{
    public class AjaxResult
    {
        /// <summary>
        /// 是否产生错误
        /// </summary>
        public bool IsError { get; set; }

        /// <summary>
        /// 错误信息,或者成功信息
        /// </summary>
        public string Message { get; set; }

        /// <summary>
        /// 成功可能时返回的数据
        /// </summary>
        public object Data { get; set; }

        #region Error
        public static AjaxResult Error()
        {
            return new AjaxResult
            {
                IsError = true
            };
        }
        public static AjaxResult Error(string message)
        {
            return new AjaxResult
            {
                IsError = true,
                Message = message
            };
        }
        public static AjaxResult Error(object data, string message)
        {
            return new AjaxResult
            {
                IsError = true,
                Message = message,
                Data = data
            };
        }
        #endregion

        #region Success
        public static AjaxResult Success()
        {
            return new AjaxResult
            {
                IsError = false
            };
        }
        public static AjaxResult Success(string message)
        {
            return new AjaxResult
            {
                IsError = false,
                Message = message
            };
        }
        public static AjaxResult Success(object data)
        {
            return new AjaxResult
            {
                IsError = false,
                Data = data
            };
        }
        public static AjaxResult Success(object data, string message)
        {
            return new AjaxResult
            {
                IsError = false,
                Data = data,
                Message = message
            };
        }
        #endregion

        /// <summary>
        /// 返回当前对象JSON字符串
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            return JSON.FormatString(this);
        }
    }
}

另外,你可以修改返回错误的时候,对应的Exception对象。

 

标签:返回,AjaxResult,return,自定义,C#,IsError,new,message,public
来源: https://www.cnblogs.com/mq0036/p/11630371.html