XFE Git
XFE Studio Git
Git 首页 全局搜索
XFE 主站 文档 NuGet

XFEExtension

【DLL】XFE各类拓展是一个C#的DLL库,旨在优化C#代码中常用语句的使用,并提供更简洁的访问方式,同时提供Xunit测试框架,快速搭建服务器/客户端,免费ChatGPTAPI接口,免费通讯服务器,XFE下载器,新增格式等

公开
关注 0 Fork 0 Star 0
返回提交历史

XFEstudio/XFEExtension

Delete XFE各类拓展/AttributeExtention.cs

d90be21
Tom Smith <132526994+XFEstudio@users.noreply.github.com>
提交于

代码差异

1 个文件 +0 -125
Deleted XFE各类拓展/AttributeExtention.cs +0 -125
@@ -1,125 +0,0 @@
1 using System;
2 using System.Linq;
3 using System.Reflection;
4
5 namespace XFE各类拓展.AttributeExtension
6 {
7 /// <summary>
8 /// 属性拓展类
9 /// </summary>
10 public static class AttributeExtension
11 {
12 /// <summary>
13 /// 获取该对象的给定属性的对象
14 /// </summary>
15 /// <typeparam name="T">属性类型</typeparam>
16 /// <param name="obj">对象</param>
17 public static T GetAttribute<T>(this object obj) where T : Attribute
18 {
19 if (obj is string || obj is int || obj is double || obj is float || obj is decimal || obj is DateTime || obj is bool)
20 {
21 throw new XFEExtensionException("基础类型不支持获取属性");
22 }
23 return obj.GetType().GetCustomAttributes(typeof(T), false).FirstOrDefault() as T;
24 }
25 /// <summary>
26 /// 获取给定类的某个方法的给定属性的对象
27 /// </summary>
28 /// <typeparam name="T">属性类型</typeparam>
29 /// <param name="className">类名(区分大小写)</param>
30 /// <param name="methodName">类中的方法名</param>
31 /// <returns></returns>
32 public static T GetAttributeInMethod<T>(string className, string methodName) where T : Attribute
33 {
34 var type = Assembly.GetCallingAssembly().GetType(className, false, true);
35 if (type == null)
36 {
37 throw new XFEExtensionException($"未找到类{className}");
38 }
39 else
40 {
41 var method = type.GetMethod(methodName);
42 if (method == null)
43 {
44 throw new XFEExtensionException($"未找到方法{methodName}");
45 }
46 else
47 {
48 return method.GetCustomAttributes(typeof(T), false).FirstOrDefault() as T;
49 }
50 }
51 }
52 /// <summary>
53 /// 获取给定类的某个字段的给定属性的对象
54 /// </summary>
55 /// <typeparam name="T">属性类型</typeparam>
56 /// <param name="className">类名(区分大小写)</param>
57 /// <param name="fieldName">类中的字段名称</param>
58 /// <returns></returns>
59 public static T GetAttributeInField<T>(string className, string fieldName) where T : Attribute
60 {
61 var type = Assembly.GetCallingAssembly().GetType(className, false, true);
62 if (type == null)
63 {
64 throw new XFEExtensionException($"未找到类{className}");
65 }
66 else
67 {
68 var field = type.GetField(fieldName);
69 if (field == null)
70 {
71 throw new XFEExtensionException($"未找到字段{fieldName}");
72 }
73 else
74 {
75 return field.GetCustomAttributes(typeof(T), false).FirstOrDefault() as T;
76 }
77 }
78 }
79 /// <summary>
80 /// 获取给定类的某个属性的给定属性的对象
81 /// </summary>
82 /// <typeparam name="T">属性类型</typeparam>
83 /// <param name="className">类名(区分大小写)</param>
84 /// <param name="propertyName">类中的属性名称</param>
85 /// <returns></returns>
86 public static T GetAttributeInProperty<T>(string className, string propertyName) where T : Attribute
87 {
88 var type = Assembly.GetCallingAssembly().GetType(className, false, true);
89 if (type == null)
90 {
91 throw new XFEExtensionException($"未找到类{className}");
92 }
93 else
94 {
95 var property = type.GetProperty(propertyName);
96 if (property == null)
97 {
98 throw new XFEExtensionException($"未找到属性{propertyName}");
99 }
100 else
101 {
102 return property.GetCustomAttributes(typeof(T), false).FirstOrDefault() as T;
103 }
104 }
105 }
106 /// <summary>
107 /// 给定类的给定属性的对象
108 /// </summary>
109 /// <typeparam name="T"></typeparam>
110 /// <param name="className"></param>
111 /// <returns></returns>
112 public static T GetAttributeOnClass<T>(string className) where T : Attribute
113 {
114 var type = Assembly.GetCallingAssembly().GetType(className, false, true);
115 if (type == null)
116 {
117 throw new XFEExtensionException($"未找到类{className}");
118 }
119 else
120 {
121 return type.GetCustomAttributes(typeof(T), false).FirstOrDefault() as T;
122 }
123 }
124 }
125 }