OperateResult 基础类及派生类介绍

前言


在Visual Studio 中的NuGet管理器中可以下载安装,也可以直接在NuGet控制台输入下面的指令安装:

Install-Package HslCommunication

NuGet安装教程  http://www.cnblogs.com/dathlin/p/7705014.html

联系作者及加群方式(激活码在群里发放):http://www.hslcommunication.cn/Cooperation

概述


通常我们执行一个方法的时候,方法会实现一堆复杂的功能,我们需要知道是否操作成功,如果操作失败,失败结果又是什么,失败代号又是什么。

除了上面的场景外,我们可能还会返回一个自己需要的结果值,比如一个额外的string对象,Bitmap对象,甚至是自定义的对象,使用本工具的类可以轻松实现,提供了从1一个自定义对象到10个自定义对象的派生类,以满足您日常的绝大多使用情况。

 

举例


此处定义了一个方法,传入文件路径,输出文件内容,事实上这个方法是很容易出现异常的,由于文件不存在,文件被占用,文件路径太长等等,如下的方法就可以相对优雅的读取内容:

        /// <summary>
        /// 输入一个文件名,输出文件名的内容
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns></returns>
        private HslCommunication.OperateResult<string> GetInformation(string fileName)
        {
            HslCommunication.OperateResult<string> result = new HslCommunication.OperateResult<string>();

            try
            {
                using (System.IO.StreamReader sr = new System.IO.StreamReader(fileName, Encoding.UTF8))
                {
                    result.Content = sr.ReadToEnd();
                    result.IsSuccess = true;
                }
            }
            catch(Exception ex)
            {
                result.Message = ex.Message;
            }

            return result;
        }

至于调用它,也是非常的方便:

        private void userButton10_Click(object sender, EventArgs e)
        {
            HslCommunication.OperateResult<string> result = GetInformation("D:\\123.txt");
            if(result.IsSuccess)
            {
                MessageBox.Show(result.Content);
            }
            else
            {
                MessageBox.Show("读取失败:" + result.Message);
            }
        }

 

下面就贴出这个类及派生类的源代码,方便大家使用及学习:

    /// <summary>
    /// 操作结果的类,只带有成功标志和错误信息
    /// </summary>
    public class OperateResult
    {
        /// <summary>
        /// 指示本次访问是否成功
        /// </summary>
        public bool IsSuccess { get; set; }


        /// <summary>
        /// 具体的错误描述
        /// </summary>
        public string Message { get; set; } = StringResources.UnknownError;


        /// <summary>
        /// 具体的错误代码
        /// </summary>
        public int ErrorCode { get; set; } = 10000;

        /// <summary>
        /// 允许用户自己使用的一个额外的int数据,可以根据自身的需求进行扩充
        /// </summary>
        public int CustomerCode { get; set; } = 0;


        /// <summary>
        /// 消息附带的额外信息
        /// </summary>
        public object Tag { get; set; }


        /// <summary>
        /// 获取错误代号及文本描述
        /// </summary>
        /// <returns></returns>
        public string ToMessageShowString()
        {
            return $"{StringResources.ErrorCode}:{ErrorCode}{Environment.NewLine}{StringResources.TextDescription}:{Message}";
        }


    }

    /// <summary>
    /// 操作结果的泛型类,允许带一个用户自定义的泛型对象,推荐使用这个类
    /// </summary>
    /// <typeparam name="T">泛型类</typeparam>
    public class OperateResult<T> : OperateResult
    {
        /// <summary>
        /// 用户自定义的泛型数据
        /// </summary>
        public T Content { get; set; }
    }

    /// <summary>
    /// 操作结果的泛型类,允许带两个用户自定义的泛型对象,推荐使用这个类
    /// </summary>
    /// <typeparam name="T1">泛型类</typeparam>
    /// <typeparam name="T2">泛型类</typeparam>
    public class OperateResult<T1, T2> : OperateResult
    {
        /// <summary>
        /// 用户自定义的泛型数据1
        /// </summary>
        public T1 Content1 { get; set; }

        /// <summary>
        /// 用户自定义的泛型数据2
        /// </summary>
        public T2 Content2 { get; set; }
    }

    /// <summary>
    /// 操作结果的泛型类,允许带三个用户自定义的泛型对象,推荐使用这个类
    /// </summary>
    /// <typeparam name="T1">泛型类</typeparam>
    /// <typeparam name="T2">泛型类</typeparam>
    /// <typeparam name="T3">泛型类</typeparam>
    public class OperateResult<T1, T2, T3> : OperateResult
    {
        /// <summary>
        /// 用户自定义的泛型数据1
        /// </summary>
        public T1 Content1 { get; set; }

        /// <summary>
        /// 用户自定义的泛型数据2
        /// </summary>
        public T2 Content2 { get; set; }
        
        /// <summary>
        /// 用户自定义的泛型数据3
        /// </summary>
        public T3 Content3 { get; set; }
    }


    /// <summary>
    /// 操作结果的泛型类,允许带四个用户自定义的泛型对象,推荐使用这个类
    /// </summary>
    /// <typeparam name="T1">泛型类</typeparam>
    /// <typeparam name="T2">泛型类</typeparam>
    /// <typeparam name="T3">泛型类</typeparam>
    /// <typeparam name="T4">泛型类</typeparam>
    public class OperateResult<T1, T2, T3, T4> : OperateResult
    {
        /// <summary>
        /// 用户自定义的泛型数据1
        /// </summary>
        public T1 Content1 { get; set; }

        /// <summary>
        /// 用户自定义的泛型数据2
        /// </summary>
        public T2 Content2 { get; set; }
        
        /// <summary>
        /// 用户自定义的泛型数据3
        /// </summary>
        public T3 Content3 { get; set; }

        /// <summary>
        /// 用户自定义的泛型数据4
        /// </summary>
        public T4 Content4 { get; set; }
    }


    /// <summary>
    /// 操作结果的泛型类,允许带五个用户自定义的泛型对象,推荐使用这个类
    /// </summary>
    /// <typeparam name="T1">泛型类</typeparam>
    /// <typeparam name="T2">泛型类</typeparam>
    /// <typeparam name="T3">泛型类</typeparam>
    /// <typeparam name="T4">泛型类</typeparam>
    /// <typeparam name="T5">泛型类</typeparam>
    public class OperateResult<T1, T2, T3, T4, T5> : OperateResult
    {
        /// <summary>
        /// 用户自定义的泛型数据1
        /// </summary>
        public T1 Content1 { get; set; }

        /// <summary>
        /// 用户自定义的泛型数据2
        /// </summary>
        public T2 Content2 { get; set; }
        
        /// <summary>
        /// 用户自定义的泛型数据3
        /// </summary>
        public T3 Content3 { get; set; }

        /// <summary>
        /// 用户自定义的泛型数据4
        /// </summary>
        public T4 Content4 { get; set; }

        /// <summary>
        /// 用户自定义的泛型数据5
        /// </summary>
        public T5 Content5 { get; set; }
        
    }

    /// <summary>
    /// 操作结果的泛型类,允许带六个用户自定义的泛型对象,推荐使用这个类
    /// </summary>
    /// <typeparam name="T1">泛型类</typeparam>
    /// <typeparam name="T2">泛型类</typeparam>
    /// <typeparam name="T3">泛型类</typeparam>
    /// <typeparam name="T4">泛型类</typeparam>
    /// <typeparam name="T5">泛型类</typeparam>
    /// <typeparam name="T6">泛型类</typeparam>
    public class OperateResult<T1, T2, T3, T4, T5, T6> : OperateResult
    {
        /// <summary>
        /// 用户自定义的泛型数据1
        /// </summary>
        public T1 Content1 { get; set; }

        /// <summary>
        /// 用户自定义的泛型数据2
        /// </summary>
        public T2 Content2 { get; set; }
        
        /// <summary>
        /// 用户自定义的泛型数据3
        /// </summary>
        public T3 Content3 { get; set; }

        /// <summary>
        /// 用户自定义的泛型数据4
        /// </summary>
        public T4 Content4 { get; set; }

        /// <summary>
        /// 用户自定义的泛型数据5
        /// </summary>
        public T5 Content5 { get; set; }
        
        /// <summary>
        /// 用户自定义的泛型数据5
        /// </summary>
        public T6 Content6 { get; set; }
        
    }

    /// <summary>
    /// 操作结果的泛型类,允许带七个用户自定义的泛型对象,推荐使用这个类
    /// </summary>
    /// <typeparam name="T1">泛型类</typeparam>
    /// <typeparam name="T2">泛型类</typeparam>
    /// <typeparam name="T3">泛型类</typeparam>
    /// <typeparam name="T4">泛型类</typeparam>
    /// <typeparam name="T5">泛型类</typeparam>
    /// <typeparam name="T6">泛型类</typeparam>
    /// <typeparam name="T7">泛型类</typeparam>
    public class OperateResult<T1, T2, T3, T4, T5, T6, T7> : OperateResult
    {
        /// <summary>
        /// 用户自定义的泛型数据1
        /// </summary>
        public T1 Content1 { get; set; }

        /// <summary>
        /// 用户自定义的泛型数据2
        /// </summary>
        public T2 Content2 { get; set; }
        
        /// <summary>
        /// 用户自定义的泛型数据3
        /// </summary>
        public T3 Content3 { get; set; }

        /// <summary>
        /// 用户自定义的泛型数据4
        /// </summary>
        public T4 Content4 { get; set; }

        /// <summary>
        /// 用户自定义的泛型数据5
        /// </summary>
        public T5 Content5 { get; set; }
        
        /// <summary>
        /// 用户自定义的泛型数据6
        /// </summary>
        public T6 Content6 { get; set; }
        
        /// <summary>
        /// 用户自定义的泛型数据7
        /// </summary>
        public T7 Content7 { get; set; }


    }

    /// <summary>
    /// 操作结果的泛型类,允许带八个用户自定义的泛型对象,推荐使用这个类
    /// </summary>
    /// <typeparam name="T1">泛型类</typeparam>
    /// <typeparam name="T2">泛型类</typeparam>
    /// <typeparam name="T3">泛型类</typeparam>
    /// <typeparam name="T4">泛型类</typeparam>
    /// <typeparam name="T5">泛型类</typeparam>
    /// <typeparam name="T6">泛型类</typeparam>
    /// <typeparam name="T7">泛型类</typeparam>
    /// <typeparam name="T8">泛型类</typeparam>
    public class OperateResult<T1, T2, T3, T4, T5, T6, T7, T8> : OperateResult
    {
        /// <summary>
        /// 用户自定义的泛型数据1
        /// </summary>
        public T1 Content1 { get; set; }

        /// <summary>
        /// 用户自定义的泛型数据2
        /// </summary>
        public T2 Content2 { get; set; }
        
        /// <summary>
        /// 用户自定义的泛型数据3
        /// </summary>
        public T3 Content3 { get; set; }

        /// <summary>
        /// 用户自定义的泛型数据4
        /// </summary>
        public T4 Content4 { get; set; }

        /// <summary>
        /// 用户自定义的泛型数据5
        /// </summary>
        public T5 Content5 { get; set; }
        
        /// <summary>
        /// 用户自定义的泛型数据6
        /// </summary>
        public T6 Content6 { get; set; }
        
        /// <summary>
        /// 用户自定义的泛型数据7
        /// </summary>
        public T7 Content7 { get; set; }

        /// <summary>
        /// 用户自定义的泛型数据8
        /// </summary>
        public T8 Content8 { get; set; }
    }

    /// <summary>
    /// 操作结果的泛型类,允许带九个用户自定义的泛型对象,推荐使用这个类
    /// </summary>
    /// <typeparam name="T1">泛型类</typeparam>
    /// <typeparam name="T2">泛型类</typeparam>
    /// <typeparam name="T3">泛型类</typeparam>
    /// <typeparam name="T4">泛型类</typeparam>
    /// <typeparam name="T5">泛型类</typeparam>
    /// <typeparam name="T6">泛型类</typeparam>
    /// <typeparam name="T7">泛型类</typeparam>
    /// <typeparam name="T8">泛型类</typeparam>
    /// <typeparam name="T9">泛型类</typeparam>
    public class OperateResult<T1, T2, T3, T4, T5, T6, T7, T8, T9> : OperateResult
    {
        /// <summary>
        /// 用户自定义的泛型数据1
        /// </summary>
        public T1 Content1 { get; set; }

        /// <summary>
        /// 用户自定义的泛型数据2
        /// </summary>
        public T2 Content2 { get; set; }
        
        /// <summary>
        /// 用户自定义的泛型数据3
        /// </summary>
        public T3 Content3 { get; set; }

        /// <summary>
        /// 用户自定义的泛型数据4
        /// </summary>
        public T4 Content4 { get; set; }

        /// <summary>
        /// 用户自定义的泛型数据5
        /// </summary>
        public T5 Content5 { get; set; }
        
        /// <summary>
        /// 用户自定义的泛型数据6
        /// </summary>
        public T6 Content6 { get; set; }
        
        /// <summary>
        /// 用户自定义的泛型数据7
        /// </summary>
        public T7 Content7 { get; set; }

        /// <summary>
        /// 用户自定义的泛型数据8
        /// </summary>
        public T8 Content8 { get; set; }

        /// <summary>
        /// 用户自定义的泛型数据9
        /// </summary>
        public T9 Content9 { get; set; }
    }

    /// <summary>
    /// 操作结果的泛型类,允许带十个用户自定义的泛型对象,推荐使用这个类
    /// </summary>
    /// <typeparam name="T1">泛型类</typeparam>
    /// <typeparam name="T2">泛型类</typeparam>
    /// <typeparam name="T3">泛型类</typeparam>
    /// <typeparam name="T4">泛型类</typeparam>
    /// <typeparam name="T5">泛型类</typeparam>
    /// <typeparam name="T6">泛型类</typeparam>
    /// <typeparam name="T7">泛型类</typeparam>
    /// <typeparam name="T8">泛型类</typeparam>
    /// <typeparam name="T9">泛型类</typeparam>
    /// <typeparam name="T10">泛型类</typeparam>
    public class OperateResult<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10> : OperateResult
    {
        /// <summary>
        /// 用户自定义的泛型数据1
        /// </summary>
        public T1 Content1 { get; set; }

        /// <summary>
        /// 用户自定义的泛型数据2
        /// </summary>
        public T2 Content2 { get; set; }
        
        /// <summary>
        /// 用户自定义的泛型数据3
        /// </summary>
        public T3 Content3 { get; set; }

        /// <summary>
        /// 用户自定义的泛型数据4
        /// </summary>
        public T4 Content4 { get; set; }

        /// <summary>
        /// 用户自定义的泛型数据5
        /// </summary>
        public T5 Content5 { get; set; }
        
        /// <summary>
        /// 用户自定义的泛型数据6
        /// </summary>
        public T6 Content6 { get; set; }
        
        /// <summary>
        /// 用户自定义的泛型数据7
        /// </summary>
        public T7 Content7 { get; set; }

        /// <summary>
        /// 用户自定义的泛型数据8
        /// </summary>
        public T8 Content8 { get; set; }

        /// <summary>
        /// 用户自定义的泛型数据9
        /// </summary>
        public T9 Content9 { get; set; }

        /// <summary>
        /// 用户自定义的泛型数据10
        /// </summary>
        public T10 Content10 { get; set; }
    }

 

posted @ 2017-11-20 12:04  dathlin  阅读(4137)  评论(3编辑  收藏  举报