forked from tmoonlight/NSmartProxy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHttpResult.cs
More file actions
64 lines (57 loc) · 1.61 KB
/
HttpResult.cs
File metadata and controls
64 lines (57 loc) · 1.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
using System;
using System.Collections.Generic;
using System.Text;
namespace NSmartProxy.Data
{
public static class HttpResultExtension
{
/// <summary>
/// 包装对象为返回结果,即使是null也能包装
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="obj"></param>
/// <returns></returns>
public static HttpResult<T> Wrap<T>(this T obj)
{
if (obj is Exception)
{
Exception ex = obj as Exception;
return new HttpResult<T>()
{
State = 0,
Msg = ex.ToString()
};
}
return new HttpResult<T>()
{
State = 1,
Msg = "",
Data = obj
};
}
}
public class HttpResult<T>
{
private static HttpResult<T> nullSuccessResult;
public HttpResult()
{
}
//1代表成功 0代表失败
private int state;
private string msg;
private T data;
public int State { get => state; set => state = value; }
/// <summary>
/// 附加信息,一般是错误信息,或者是提示信息
/// </summary>
public string Msg { get => msg; set => msg = value; }
public T Data { get => data; set => data = value; }
public static HttpResult<T> NullSuccessResult
{
get
{
return nullSuccessResult ?? new HttpResult<T>() { state = 1 };
}
}
}
}