容朕想想 发表于 2023-5-17 16:34:08

Natasha相关辅助类 (六)

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)}");
      }
            
    }
}运行结果:

AvailableNameReverser.GetAvailableName

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

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 "";
}结果截图:

TypeNatashaExtension

Natasha的类型拓展
类名参数返回说明IsImplementFromthis Type ,Type iTypebool当前类是否实现了某接口,iType为接口类型IsImplementFromthis TypeboolT为接口类型GetRuntimeNamethis Typestring获取运行时类名GetDevelopNamethis Typestring获取完整类名GetDevelopNameWithoutFlagthis Typestring同GetDevelopNameGetAvailableNamethis Typestring将类名替换成 文件名可使用的名字IsSimpleTypethis Typebool判断是否为值类型,字符串类型,委托类型,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方法");
      }
}结果截图:




来源:https://www.cnblogs.com/wanghun315/archive/2023/05/17/17409141.html
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!
页: [1]
查看完整版本: Natasha相关辅助类 (六)