Blame view

netcore/src/Infrastructure/NCC/UnifyResult/Attributes/UnifyResultAttribute.cs 1.58 KB
de2bd2f9   “wangming”   项目初始化
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
  using NCC.Dependency;
  using NCC.Extensions;
  using NCC.UnifyResult;
  using Microsoft.AspNetCore.Http;
  using System;
  
  namespace Microsoft.AspNetCore.Mvc
  {
      /// <summary>
      /// 规范化结果配置
      /// </summary>
      [SuppressSniffer, AttributeUsage(AttributeTargets.Method, AllowMultiple = true, Inherited = true)]
      public class UnifyResultAttribute : ProducesResponseTypeAttribute
      {
          /// <summary>
          /// 构造函数
          /// </summary>
          /// <param name="statusCode"></param>
          public UnifyResultAttribute(int statusCode) : base(statusCode)
          {
          }
  
          /// <summary>
          /// 构造函数
          /// </summary>
          /// <param name="type"></param>
          public UnifyResultAttribute(Type type) : base(type, StatusCodes.Status200OK)
          {
              WrapType(type);
          }
  
          /// <summary>
          /// 构造函数
          /// </summary>
          /// <param name="type"></param>
          /// <param name="statusCode"></param>
          public UnifyResultAttribute(Type type, int statusCode) : base(type, statusCode)
          {
              WrapType(type);
          }
  
          /// <summary>
          /// 包装类型
          /// </summary>
          /// <param name="type"></param>
          private void WrapType(Type type)
          {
              if (type != null)
              {
                  if (!type.HasImplementedRawGeneric(UnifyContext.RESTfulResultType))
                  {
                      Type = UnifyContext.RESTfulResultType.MakeGenericType(type);
                  }
                  else Type = default;
              }
          }
      }
  }