Natasha相关辅助类 (六)

  • Natasha相关辅助类 (六)已关闭评论
  • 165 次浏览
  • A+
所属分类:.NET技术
摘要

获得相关类的访问级别AccessReverser.GetAccess() 可以是泛型AccessReverser.GetAccess方法中的参数可以是


AccessReverser.GetAccess

获得相关类的访问级别

AccessReverser.GetAccess() 可以是泛型

AccessReverser.GetAccess方法中的参数可以是

属性名称 说明
PropertyInfo 获取属性的访问级别
MethodInfo 获取方法的访问级别
EventInfo 获取事件的访问级别
FieldInfo 获取字段的访问级别
Type 获取类型的访问级别
T 获取泛型的访问级别
internal class Demo6 {     public String Field  = "";     public void Domain()     {         NatashaManagement.Preheating();         //获取访问级别         String strAccess = AccessReverser.GetAccess<Object>();         // 获取Type         Type ty = typeof(Demo6);         // 获取Demo6的访问级别         Console.WriteLine($"Type Access is {AccessReverser.GetAccess(ty)}");         // 获取Field字段         FieldInfo? fieldInfo = ty.GetField("Field");         if (fieldInfo != null) {             //输出Field字段的访问级别             Console.WriteLine($"MethodText Access is {AccessReverser.GetAccess(fieldInfo)}");         }         // 获取MethodText字段         MethodInfo? domain = ty.GetMethod("Domain");         if (domain != null) {             //输出MethodText字段的访问级别             Console.WriteLine($"MethodText Access is {AccessReverser.GetAccess(domain)}");         }                  } } 

运行结果:

Natasha相关辅助类 (六)

AvailableNameReverser.GetAvailableName

获取Type的可用名,参数为Type

// 结果为:namespace+类名 Console.WriteLine($"获取Type的可用名:{AvailableNameReverser.GetAvailableName(ty)}"); 

运行结果:

Natasha相关辅助类 (六)

DeclarationReverser.GetMethodDeclaration

展示函数信息,参数为MethodInfo

public void Domain() {     //MethodInfo? domain = ty.GetMethod("Domain");以下部分修改     // 获取Test方法     MethodInfo? test = ty.GetMethod("Test");     if (test != null) {         Console.WriteLine($"函数信息: {DeclarationReverser.GetMethodDeclaration(test)}");         //输出MethodText字段的访问级别         Console.WriteLine($"Test Access is {AccessReverser.GetAccess(test)}");     } } public String Test(in String arg1, out int arg2, ref float arg3) {     arg2 = 0;     return ""; } 

结果截图:

Natasha相关辅助类 (六)

TypeNatashaExtension

Natasha的类型拓展

类名 参数 返回 说明
IsImplementFrom this Type ,Type iType bool 当前类是否实现了某接口,iType为接口类型
IsImplementFrom this Type bool T为接口类型
GetRuntimeName this Type string 获取运行时类名
GetDevelopName this Type string 获取完整类名
GetDevelopNameWithoutFlag this Type string 同GetDevelopName
GetAvailableName this Type string 将类名替换成 文件名可使用的名字
IsSimpleType this Type bool 判断是否为值类型,字符串类型,委托类型,Type类型,及委托的子类型其中之一
//////////////////创建的接口 public interface ITest { 	public void IClass(); } internal class Demo6: ITest{ 	public void testTypeNatashaExtension() { 		//初始化 		NatashaManagement.Preheating();         bool bImplement = typeof(Demo6).IsImplementFrom(typeof(ITest));         // Class 类的情况         Console.WriteLine("Demo6类的相关结果如下:");         Console.WriteLine($"Demo6 是否实现了 ITest 的接口:{bImplement}");         Console.WriteLine($"GetRuntimeName 结果:"{typeof(Demo6).GetRuntimeName()}"");         Console.WriteLine($"GetDevelopName 结果:"{typeof(Demo6).GetDevelopName()}"");         Console.WriteLine($"GetDevelopNameWithoutFlag 结果:"{typeof(Demo6).GetDevelopNameWithoutFlag()}"");         Console.WriteLine($"GetAvailableName 结果:"{typeof(Demo6).GetAvailableName()}"");         Console.WriteLine($"IsSimpleType 结果:"{typeof(Demo6).IsSimpleType()}"");         // Dictionary<int, String>的参数         Console.WriteLine("Dictionary<int, String> 的相关结果如下:");         Console.WriteLine($"GetRuntimeName 结果:"{typeof(Dictionary<int, String>).GetRuntimeName()}"");         Console.WriteLine($"GetDevelopName 结果:"{typeof(Dictionary<int, String>).GetDevelopName()}"");         Console.WriteLine($"GetDevelopNameWithoutFlag 结果:"{typeof(Dictionary<int, String>).GetDevelopNameWithoutFlag()}"");         Console.WriteLine($"GetAvailableName 结果:"{typeof(Dictionary<int, String>).GetAvailableName()}"");         Console.WriteLine($"IsSimpleType 结果:"{typeof(Dictionary<int, String>).IsSimpleType()}"");         // Dictionary<String, HashSet<List<String>>>的参数         Console.WriteLine("Dictionary<String, HashSet<List<String>>> 的相关结果如下:");         Console.WriteLine($"GetRuntimeName 结果:"{typeof(Dictionary<String, HashSet<List<String>>>).GetRuntimeName()}"");         Console.WriteLine($"GetDevelopName 结果:"{typeof(Dictionary<String, HashSet<List<String>>>).GetDevelopName()}"");         Console.WriteLine($"GetDevelopNameWithoutFlag 结果:"{typeof(Dictionary<String, HashSet<List<String>>>).GetDevelopNameWithoutFlag()}"");         Console.WriteLine($"GetAvailableName 结果:"{typeof(Dictionary<String, HashSet<List<String>>>).GetAvailableName()}"");         Console.WriteLine($"IsSimpleType 结果:"{typeof(Dictionary<String, HashSet<List<String>>>).IsSimpleType()}"");         }         public void IClass()         {             Console.WriteLine("实现ITest的IClass方法");         } } 

结果截图:

Natasha相关辅助类 (六)