C#通用输出类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml.Serialization;
using XiaoFeng;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using System.Data;

namespace XiaoFeng
{
    /// <summary>
    /// 输出消息
    /// Version : 1.0
    /// Create Date : 2016-12-23
    /// Author : jacky
    /// Site : www.zhuovi.com
    /// </summary>
    [Serializable]
    [XmlRoot("Root")]
    public class ResponseMessage<T>
    {
        #region 构造器
        /// <summary>
        /// 无参构造器
        /// </summary>
        public ResponseMessage() : this(ResponseState.success, "", default(T)) { }
        /// <summary>
        /// 设置信息
        /// </summary>
        /// <param name="state">状态</param>
        /// <param name="message">信息</param>
        /// <param name="data">数据</param>
        public ResponseMessage(ResponseState state, string message = "", T data = default(T))
        {
            this.state = state; this.data = data; this.message = message; 
        }
        #endregion

        #region 属性
        /// <summary>
        /// 状态
        /// </summary>
        [JsonConverter(typeof(StringEnumConverter))]
        public ResponseState state { get; set; }
        /// <summary>
        /// 消息
        /// </summary>
        public string message { get; set; }
        /// <summary>
        /// 数据 如果是DataTable 一定要有TableName值
        /// </summary>
        public T data { get; set; }
        /// <summary>
        /// 备用数据字段
        /// </summary>
        public dynamic other { get; set; }
        #endregion

        #region 方法

        #region 输出数据

        #region 输出JSON
        /// <summary>
        /// 输出JSON
        /// </summary>
        public void ToJson() { this.Write(EnumHelper.WriteType.JSON); }
        #endregion

        #region 输出XML
        /// <summary>
        /// 输出XML
        /// </summary>
        public void ToXml() { this.Write(EnumHelper.WriteType.XML); }
        #endregion

        #region 输出成功
        /// <summary>
        /// 输出成功
        /// </summary>
        /// <param name="writeType">输出类型</param>
        public void Success(EnumHelper.WriteType writeType = EnumHelper.WriteType.JSON)
        {
            this.state = ResponseState.success; this.Write(writeType);
        }
        /// <summary>
        /// 输出成功
        /// </summary>
        /// <param name="data">数据</param>
        /// <param name="writeType">输出类型</param>
        public void Success(T data, EnumHelper.WriteType writeType = EnumHelper.WriteType.JSON)
        {
            this.data = data; this.Success(writeType);
        }
        #endregion

        #region 输出错误
        /// <summary>
        /// 输出错误
        /// </summary>
        /// <param name="writeType">输出类型</param>
        public void Error(EnumHelper.WriteType writeType = EnumHelper.WriteType.JSON)
        {
            this.state = ResponseState.error; this.Write(writeType);
        }
        /// <summary>
        /// 输出错误
        /// </summary>
        /// <param name="message">错误信息</param>
        /// <param name="writeType">输出类型</param>
        public void Error(string message, EnumHelper.WriteType writeType = EnumHelper.WriteType.JSON)
        {
            this.message = message; this.Error(writeType);
        }
        #endregion

        #region 输出警告
        /// <summary>
        /// 输出警告
        /// </summary>
        /// <param name="WriteType">输出类型</param>
        public void Warning(EnumHelper.WriteType WriteType = EnumHelper.WriteType.JSON)
        {
            this.state = ResponseState.warning; this.Write(WriteType);
        }
        /// <summary>
        /// 输出警告
        /// </summary>
        /// <param name="message">警告信息</param>
        /// <param name="writeType">输出类型</param>
        public void Warning(string message, EnumHelper.WriteType writeType = EnumHelper.WriteType.JSON)
        {
            this.message = message; this.Warning(writeType);
        }
        #endregion

        #region 输出
        /// <summary>
        /// 输出信息
        /// </summary>
        /// <param name="data">数据</param>
        /// <param name="writeType">输出类型</param>
        public void Write(T data, EnumHelper.WriteType writeType = EnumHelper.WriteType.JSON)
        {
            this.data = data; this.Write(writeType);
        }
        /// <summary>
        /// 输出信息
        /// </summary>
        /// <param name="writeType">输出类型 默认 JSON ,XML,String</param>
        public void Write(EnumHelper.WriteType writeType = EnumHelper.WriteType.JSON)
        {
            HttpResponse Response= HttpContext.Current.Response;
            Response.Clear();
            Response.ClearContent();
            Response.ClearHeaders();
            Response.ContentEncoding = System.Text.Encoding.UTF8;
            System.Text.StringBuilder sbr = new System.Text.StringBuilder();
            switch (writeType)
            {
                case EnumHelper.WriteType.JSON:
                    sbr.Append(JsonConvert.SerializeObject(this));
                    Response.ContentType = "text/json";
                    break;
                case EnumHelper.WriteType.XML:
                    Response.ContentType = "text/xml";
                    if (this.data.GetType() == typeof(DataTable))
                    {
                        if ((this.data as DataTable).TableName.IsNullOrEmpty())
                            (this.data as DataTable).TableName = "item";
                    }
                    sbr.Append(XmlConvert.SerializerObject<ResponseMessage<T>>(this, "UTF-8", true));
                    sbr.Append("\n<!--生成于[{0}]-->".format(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")));
                    break;
                case EnumHelper.WriteType.String:
                    Response.ContentType = "text/plain";
                    sbr.Append(String.Format("state:{0}\nmessage:{1}", this.state.ToString(), this.message.ToString()));
                    break;
            }
            Response.Write(sbr.ToString());
            Response.Flush();
            Response.End();
        }
        #endregion

        #endregion

        #region 返回序列化数据
        /// <summary>
        /// 返回序列化数据
        /// </summary>
        /// <param name="writeType">输出类型 默认 JSON ,XML,String</param>
        /// <returns></returns>
        public string getData(XiaoFeng.EnumHelper.WriteType writeType = XiaoFeng.EnumHelper.WriteType.JSON)
        {
            switch (writeType)
            {
                case EnumHelper.WriteType.JSON:
                    return JsonConvert.SerializeObject(this);
                case EnumHelper.WriteType.XML:
                    if (this.data.GetType() == typeof(DataTable))
                    {
                        if ((this.data as DataTable).TableName.IsNullOrEmpty())
                            (this.data as DataTable).TableName = "item";
                    }
                    return XmlConvert.SerializerObject<ResponseMessage<T>>(this, "UTF-8", true);
                case EnumHelper.WriteType.String:
                    return String.Format("state:{0}\nmessage:{1}", this.state.ToString(), this.message.ToString());
                default:
                    return "";
            }
        }
        #endregion

        #endregion
    }

    #region 消息状态
    /// <summary>
    /// 消息状态
    /// </summary>
    public enum ResponseState
    {
        /// <summary>
        ///
        /// </summary>
        none = 0,
        /// <summary>
        /// 成功
        /// </summary>
        success = 200,
        /// <summary>
        /// 出错
        /// </summary>
        error = 500,
        /// <summary>
        /// 警告
        /// </summary>
        warning = 100
    }
    #endregion

    #region 输出消息
    /// <summary>
    /// 输出消息
    /// Version : 1.0
    /// Create Date : 2016-12-23
    /// Author : jacky
    /// Site : www.zhuovi.com
    /// </summary>
    [Serializable]
    [XmlRoot("Root")]
    public class ResponseMessage : ResponseMessage<String>
    {
        #region 构造器
        /// <summary>
        /// 无参构造器
        /// </summary>
        public ResponseMessage() : this(ResponseState.success, "", "") { }
        /// <summary>
        /// 构造器
        /// </summary>
        /// <param name="state">状态</param>
        /// <param name="message">消息</param>
        /// <param name="data">数据</param>
        public ResponseMessage(ResponseState state, string message = "", String data = "") : base(state, message, data) { }
        #endregion
    }
    #endregion
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值