翼度科技»论坛 编程开发 .net 查看内容

Natasha相关辅助类 (六)

6

主题

6

帖子

18

积分

新手上路

Rank: 1

积分
18
AccessReverser.GetAccess

获得相关类的访问级别
AccessReverser.GetAccess() 可以是泛型
AccessReverser.GetAccess方法中的参数可以是
属性名称说明PropertyInfo获取属性的访问级别MethodInfo获取方法的访问级别EventInfo获取事件的访问级别FieldInfo获取字段的访问级别Type获取类型的访问级别T获取泛型的访问级别
  1. internal class Demo6
  2. {
  3.     public String Field  = "";
  4.     public void Domain()
  5.     {
  6.         NatashaManagement.Preheating();
  7.         //获取访问级别
  8.         String strAccess = AccessReverser.GetAccess<Object>();
  9.         // 获取Type
  10.         Type ty = typeof(Demo6);
  11.         // 获取Demo6的访问级别
  12.         Console.WriteLine($"Type Access is {AccessReverser.GetAccess(ty)}");
  13.         // 获取Field字段
  14.         FieldInfo? fieldInfo = ty.GetField("Field");
  15.         if (fieldInfo != null) {
  16.             //输出Field字段的访问级别
  17.             Console.WriteLine($"MethodText Access is {AccessReverser.GetAccess(fieldInfo)}");
  18.         }
  19.         // 获取MethodText字段
  20.         MethodInfo? domain = ty.GetMethod("Domain");
  21.         if (domain != null) {
  22.             //输出MethodText字段的访问级别
  23.             Console.WriteLine($"MethodText Access is {AccessReverser.GetAccess(domain)}");
  24.         }
  25.             
  26.     }
  27. }
复制代码
运行结果:

AvailableNameReverser.GetAvailableName

获取Type的可用名,参数为Type
  1. // 结果为:namespace+类名
  2. Console.WriteLine($"获取Type的可用名:{AvailableNameReverser.GetAvailableName(ty)}");
复制代码
运行结果:

DeclarationReverser.GetMethodDeclaration

展示函数信息,参数为MethodInfo
  1. public void Domain()
  2. {
  3.     //MethodInfo? domain = ty.GetMethod("Domain");以下部分修改
  4.     // 获取Test方法
  5.     MethodInfo? test = ty.GetMethod("Test");
  6.     if (test != null) {
  7.         Console.WriteLine($"函数信息: {DeclarationReverser.GetMethodDeclaration(test)}");
  8.         //输出MethodText字段的访问级别
  9.         Console.WriteLine($"Test Access is {AccessReverser.GetAccess(test)}");
  10.     }
  11. }
  12. public String Test(in String arg1, out int arg2, ref float arg3) {
  13.     arg2 = 0;
  14.     return "";
  15. }
复制代码
结果截图:

TypeNatashaExtension

Natasha的类型拓展
类名参数返回说明IsImplementFromthis Type ,Type iTypebool当前类是否实现了某接口,iType为接口类型IsImplementFromthis TypeboolT为接口类型GetRuntimeNamethis Typestring获取运行时类名GetDevelopNamethis Typestring获取完整类名GetDevelopNameWithoutFlagthis Typestring同GetDevelopNameGetAvailableNamethis Typestring将类名替换成 文件名可使用的名字IsSimpleTypethis Typebool判断是否为值类型,字符串类型,委托类型,Type类型,及委托的子类型其中之一
  1. //////////////////创建的接口
  2. public interface ITest {
  3.         public void IClass();
  4. }
  5. internal class Demo6: ITest{
  6.         public void testTypeNatashaExtension() {
  7.                 //初始化
  8.                 NatashaManagement.Preheating();
  9.         bool bImplement = typeof(Demo6).IsImplementFrom(typeof(ITest));
  10.         // Class 类的情况
  11.         Console.WriteLine("Demo6类的相关结果如下:");
  12.         Console.WriteLine($"Demo6 是否实现了 ITest 的接口:{bImplement}");
  13.         Console.WriteLine($"GetRuntimeName 结果:"{typeof(Demo6).GetRuntimeName()}"");
  14.         Console.WriteLine($"GetDevelopName 结果:"{typeof(Demo6).GetDevelopName()}"");
  15.         Console.WriteLine($"GetDevelopNameWithoutFlag 结果:"{typeof(Demo6).GetDevelopNameWithoutFlag()}"");
  16.         Console.WriteLine($"GetAvailableName 结果:"{typeof(Demo6).GetAvailableName()}"");
  17.         Console.WriteLine($"IsSimpleType 结果:"{typeof(Demo6).IsSimpleType()}"");
  18.         // Dictionary<int, String>的参数
  19.         Console.WriteLine("Dictionary<int, String> 的相关结果如下:");
  20.         Console.WriteLine($"GetRuntimeName 结果:"{typeof(Dictionary<int, String>).GetRuntimeName()}"");
  21.         Console.WriteLine($"GetDevelopName 结果:"{typeof(Dictionary<int, String>).GetDevelopName()}"");
  22.         Console.WriteLine($"GetDevelopNameWithoutFlag 结果:"{typeof(Dictionary<int, String>).GetDevelopNameWithoutFlag()}"");
  23.         Console.WriteLine($"GetAvailableName 结果:"{typeof(Dictionary<int, String>).GetAvailableName()}"");
  24.         Console.WriteLine($"IsSimpleType 结果:"{typeof(Dictionary<int, String>).IsSimpleType()}"");
  25.         // Dictionary<String, HashSet<List<String>>>的参数
  26.         Console.WriteLine("Dictionary<String, HashSet<List<String>>> 的相关结果如下:");
  27.         Console.WriteLine($"GetRuntimeName 结果:"{typeof(Dictionary<String, HashSet<List<String>>>).GetRuntimeName()}"");
  28.         Console.WriteLine($"GetDevelopName 结果:"{typeof(Dictionary<String, HashSet<List<String>>>).GetDevelopName()}"");
  29.         Console.WriteLine($"GetDevelopNameWithoutFlag 结果:"{typeof(Dictionary<String, HashSet<List<String>>>).GetDevelopNameWithoutFlag()}"");
  30.         Console.WriteLine($"GetAvailableName 结果:"{typeof(Dictionary<String, HashSet<List<String>>>).GetAvailableName()}"");
  31.         Console.WriteLine($"IsSimpleType 结果:"{typeof(Dictionary<String, HashSet<List<String>>>).IsSimpleType()}"");
  32.         }
  33.         public void IClass()
  34.         {
  35.             Console.WriteLine("实现ITest的IClass方法");
  36.         }
  37. }
复制代码
结果截图:




来源:https://www.cnblogs.com/wanghun315/archive/2023/05/17/17409141.html
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x

举报 回复 使用道具