当前位置: 首页 > news >正文

网站seo评测seo在线优化网站

网站seo评测,seo在线优化网站,做自由行的网站好,佛山网站制作系统【.NET Core】反射(Reflection)详解(二) 文章目录 【.NET Core】反射(Reflection)详解(二)一、概述二、Type类2.1 Type对象表示哪些类型2.2 获取Type及其关联对象类型的方式2.3 Type…

【.NET Core】反射(Reflection)详解(二)

文章目录

    • 【.NET Core】反射(Reflection)详解(二)
    • 一、概述
    • 二、Type类
      • 2.1 Type对象表示哪些类型
      • 2.2 获取Type及其关联对象类型的方式
      • 2.3 Type.FilterName字段
      • 2.4 Type.FilterNameIgnoreCase字段
      • 2.5 Type.Assembly属性
      • 2.6 Type.BaseType属性
      • 2.7 Type.FullName属性
      • 2.8 Type.Module属性
      • 2.9 Type.Namespace属性
      • 2.10 Type.FindMembers方法
      • 2.11 Type.GetConstructor方法
      • 2.12 Type.GetField 方法
      • 2.13 Type.GetMember方法
      • 2.14 Type.GetMethod 方法
      • 2.15 Type.GetProperty 方法
      • 2.16 Type.InvokeMember方法
    • 二、总结

一、概述

反射提供了对已封装的程序集、模型和类型的对象一种动态访问方法。反射包含动态加载程序集的Assembly类、程序集中模块访问的Module类、对类信息Type类、构造函数信息ConstructorInfo类、方法信息MethodInfo类、字段信息FieldInfo类、事件信息EventInfo类、属性信息PropertyInfo类、参数信息ParameterInfo类。博文《反射(Reflection)详解(一)》已详细讲解了Assembly类、Module类的用法。本章将重点讲解Type类

二、Type类

Type类型声明:类类型、接口类型、数组类型、值类型、枚举类型、类型参数、泛型类型定义以及开发或封闭构造的泛型类型。

Type是功能的根类。System.Relection是访问元数据的主要方式。使用的成员Type获取有关类型声明、类型的构造函数、方法、字段、属性和事件以及在其中部署类的模块和程序集。Type无需任何权限即可通过反射获取有关类型及其成员信息。

2.1 Type对象表示哪些类型

此类是线程安全的,多个线程可以并发读取此类型的实例。类Type实例可以表示以下任一类型:

  • 值类型
  • 数组
  • 接口
  • 枚举
  • 委托
  • 构造泛型类型和泛型类型定义
  • 构造泛型类型、泛型类型定义和泛型方法定义的类型参数和类型参数

2.2 获取Type及其关联对象类型的方式

  • 实例化对象通过Object.GetType方法返回一个Type对象,该对象表示实例化类型。由于Object是所有托管类型的基类,因此任何类型的实例都可以调用GetType方法。

    StringHelper stringHelper= new StringHelper();       
    Type type= typeof(StringHelper);
    String str = new String("abdce")
    Type type1 =  str.GetType();
    
  • 静态typeof()返回一个Type对象,该对象表示由其完全限定名称指定的类型。

    Type typeHelper= typeof(StringHelper);
    Type typeString = typeof(String);
    

2.3 Type.FilterName字段

表示用于名称的区分大小写的成员筛选器,此字段为只读。此字段对方法使用的委托的FindMembers引用。此委托封装的方法采用两个参数:第一个MemberInfo是对象,第二个Object是确定匹配条件。Object分配了一个字符串值,可使用通配符(*),这个通配符仅支持结束字符串匹配。

Type typeHelper = typeof(StringHelper);
MemberInfo[] members = typeHelper.FindMembers(MemberTypes.Constructor|MemberTypes.Method,BindingFlags.Public    |BindingFlags.Static    |BindingFlags.NonPublic |BindingFlags.Instance  |BindingFlags.DeclaredOnly,Type.FilterName, "GetStr*");

2.4 Type.FilterNameIgnoreCase字段

表示用于名称的不区分大小写的成员筛选器。此字段为只读。此字段对方法使用的委托的FindMembers引用。此委托封装的方法采用两个参数:第一个MemberInfo是对象,第二个Object是MemberInfo与指定的Object条件是否匹配。Object与Type.FilterName一样,分配了一个统配了一个字符串值,其中可包含结尾通配符(*);

Type typeHelper = typeof(StringHelper);
MemberInfo[] members = typeHelper.FindMembers(MemberTypes.Constructor|MemberTypes.Method,BindingFlags.Public    |BindingFlags.Static    |BindingFlags.NonPublic |BindingFlags.Instance  |BindingFlags.DeclaredOnly,Type.FilterName, "getstr*");

2.5 Type.Assembly属性

获取在其中声明该类型的Assembly,对于泛型类型、则获取在其中定义该泛型类型的Assembly。

Type typeHelper = typeof(StringHelper);
Console.WriteLine($"Assembly full name:{typeHelper.Assembly.FullName}");
Console.WriteLine($"Assembly QualifiedName:{typeHelper.Assembly.AssemblyQualifiedName}");

2.6 Type.BaseType属性

获取当前Type直接从中继承的类型。

Type t = typeof(int);
Console.WriteLine($"int inherits from {t.BaseType}");

2.7 Type.FullName属性

获取当前Type的名称,包含namespace但是不包含Assembly

Type t = typeof(int);
Console.WriteLine($"int inherits from {t.FullName}");

2.8 Type.Module属性

获取在其中定义当前Type的模块(DLL)

Type typeHelper = typeof(int);
Console.WriteLine($"Module QualifiedName:{typeHelper.Module.FullyQualifiedName}");

显示内容

Module QualifiedName:C:\Program Files\dotnet\shared\Microsoft.NETCore.App\7.0.3\System.Private.CoreLib.dll

2.9 Type.Namespace属性

获取当前命名空间,如果当前Type没有命名空间或表示泛型的参数,则为空。

Type typeHelper = typeof(int);
Console.WriteLine($"The Namespace:{type.Namespace}");

2.10 Type.FindMembers方法

返回指定成员类型的MemberInfo对象的筛选数组。

参数MemberTypes

说明
Construsctor1指定成员是构造函数
Custom64指定成员为自定义成员类型
All191指定所有的成员类型
Event2指定成员为事件
Field4指定成员为字段
Method8指定成员为方法
NestedType128指定成员为嵌套类型
Property16指定成员为属性
TypeInfo32指定成员为Type

2.11 Type.GetConstructor方法

用指定绑定约束和指定调用约束,搜索其参数与指定参数类型及修饰符匹配的构造函数。

public System.Reflection.ConstructorInfo? GetConstructor (System.Reflection.BindingFlags bindingAttr, System.Reflection.Binder? binder, System.Reflection.CallingConventions callConvention, Type[] types, System.Reflection.ParameterModifier[]? modifiers);

参数

bindingAttr BindingFlags

枚举值的按位组合,这些值指定如何进行搜索 BindingFlags枚举参考

binder Binder

一个对象,该对象定义一组属性并启用绑定,而绑定可能涉及选择重载方法,强制参数类型和通过反射调用成员。

callConvention CallingConventions

对象,用于指定要使用的一套规则,这些规则涉及参数的顺序和布局、传递返回值的方式,用于参数的寄存器和清理堆栈的方式。

type Type[]

Type对象的数组,表示构造函数要获取的参数的个数,顺序和类型。

modifiers ParameterModifier[]

ParameterModifier对象的数组,表示与types数组中的相应元素关联的特性。

返回

ConstructorInfo

表示符合指定需求的构造函数的对象;否则为null

2.12 Type.GetField 方法

获取当前Type的特定字段

GetField(String) : 搜索具有指定名称的公共字段。

GetField(String,BindingFlags) : 使用指定绑定约束搜索指定字段。

Type myTypeA = typeof(MyFieldClassA);
FieldInfo myFieldInfo = myTypeA.GetField("Field");

2.13 Type.GetMember方法

获取当前类型的指定成员。

GetMember(String) : 搜索具有指定名称的公共成员

GetMember(String,BindingFlags) : 使用指定的绑定约束搜索指定成员

GetMember(String,MemberTypes,BindingFlags) : 使用指定的绑定约束搜索指定成员类型的指定成员。

String myString = "GetMember_String";
Type myType = myString.GetType();
MemberInfo[] myMembers = myType.GetMember("C*");

2.14 Type.GetMethod 方法

获取当前Type的特定方法。

GetMethod(String,Int32,BindingFlags,Binder,CallingConventions,Type[],ParameterModifier[])

使用指定的绑定约束和指定的调用约定搜索其参数与指定泛型参数计数、参数类型及修饰符匹配的指定方法。

GetMethod(String, BindingFlags, Binder, CallingConventions, Type[], ParameterModifier[])

用指定的绑定约束和指定的调用约定,搜索参数与指定的参数类型及修饰符相匹配的指定方法。

GetMethod(String,Int32,BindingFlags,Binderm,Type[],ParameterModifier[])

使用指定绑定约束,搜索其参数与指定泛型参数计数、参数类型及修饰符匹配的指定方法。

GetMethod(String)

搜索具有指定名称的公共方法

StringHelper stringHelper = new StringHelper();
Type type = typeof(StringHelper);
var methodInfos = type.GetMethods(BindingFlags.Instance | BindingFlags.Public);
MethodInfo methodInfo= methodInfos?.First();
Console.WriteLine($"The methodInfo :{methodInfo.Name}");

2.15 Type.GetProperty 方法

获取当前Type的特定属性。

  • GetProperty(String,Type,Type[])

    搜索其参数与指定自变量类型匹配的指定公共属性。

  • GetProperty(String,BindingFlags,Binder,Type,Type[],ParameterModifier[])

    使用指定的绑定约束,搜索参数与指定的自变量类型及修饰符匹配的指定属性。

  • GetProperty(String,Type,Type[],ParameterModifier[])

    搜索其参数与指定自变量类型及修饰符匹配的指定公共属性

  • GetProperty(String,Type[])

    搜索其参数与指定自变量类型匹配的指定公共属性

  • GetProperty(String,Type)

    搜索具有指定名称和返回类型的公共属性

  • GetProperty(String)

    搜索具有指定名称的公共属性

2.16 Type.InvokeMember方法

调用当前Type的特定成员。

  • InvokeMember(String,BindingFlags,Binder,Object,Object[])

    使用指定的绑定约束并匹配指定的参数列表,调用指定成员。

  • InvokeMember(String,BindingFlags,Binder,Object,Object[],CultureInfo)

    使用指定的绑定约束和匹配的指定参数列表及区域性来调用指定成员。

  • InvokeMember(String,BindingFlags,Binder,Object,Object[],ParameterModifier,CultureInfo,String)

    当在派生类中重写时,使用指定的绑定约束并匹配指定的参数列表、修饰符和区域性,调用指定成员。

参数

name String

字符串,它包含要调用的构造函数、方法、属性或字段成员的名称。或空字符串(“”),表示调用默认成员。

invokeAttr BindingFlags

枚举值的按位组合,这些值指定如何进行搜索。访问可以是BindingFlags之一。如 PublicNonPublicPrivateInvokeMethodGetField 等。 查找类型无需指定。 如果省略查找的类型,则将使用 BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static

binder Binder

一个对象,该对象定义一组属性并启用绑定,而绑定可能涉及选择重载方法、强制参数类型和通过反射调用成员。

target Object

对其调用指定成员的对象。

args Object[]

包含传递给要调用的成员的参数数组。

StringHelper stringHelper = new StringHelper();
Type type = typeof(StringHelper);
Object obj = type .InvokeMember(null,BindingFlags.DeclaredOnly |BindingFlags.Public | BindingFlags.NonPublic |BindingFlags.Instance | BindingFlags.CreateInstance, null, null, args);
var methodInfos = type.GetMethod("RemoveLastChar");object response =   type.InvokeMember("RemoveLastChar", BindingFlags.DeclaredOnly |BindingFlags.Public | BindingFlags.NonPublic |BindingFlags.Instance | BindingFlags.InvokeMethod, null, obj, new Object[] { "This is ,水电费," ,","});Console.WriteLine($"The methodInfo :{response}");

二、总结

System.Type类是一个抽象的基类,实例化Type对象,其实就是实例化Type的一个派生类。Type是许多反射功能的入口。


文章转载自:
http://overlying.tyjp.cn
http://heroism.tyjp.cn
http://seersucker.tyjp.cn
http://shadowed.tyjp.cn
http://thickset.tyjp.cn
http://pectinose.tyjp.cn
http://dogmatical.tyjp.cn
http://ratt.tyjp.cn
http://brusque.tyjp.cn
http://monophysite.tyjp.cn
http://oncornavirus.tyjp.cn
http://limejuicer.tyjp.cn
http://dishouse.tyjp.cn
http://metamerism.tyjp.cn
http://bonbonniere.tyjp.cn
http://ovine.tyjp.cn
http://holiness.tyjp.cn
http://zillionaire.tyjp.cn
http://triptyque.tyjp.cn
http://foresight.tyjp.cn
http://protogalaxy.tyjp.cn
http://lender.tyjp.cn
http://enshrine.tyjp.cn
http://switch.tyjp.cn
http://demystify.tyjp.cn
http://fleury.tyjp.cn
http://visla.tyjp.cn
http://fiasco.tyjp.cn
http://dissolubility.tyjp.cn
http://epanisognathous.tyjp.cn
http://whose.tyjp.cn
http://tilapia.tyjp.cn
http://flossflower.tyjp.cn
http://southeastern.tyjp.cn
http://frugivore.tyjp.cn
http://broiler.tyjp.cn
http://tremellose.tyjp.cn
http://novennial.tyjp.cn
http://lymphopenia.tyjp.cn
http://phenomena.tyjp.cn
http://spait.tyjp.cn
http://cocainist.tyjp.cn
http://hairline.tyjp.cn
http://ichthyologist.tyjp.cn
http://interface.tyjp.cn
http://lumberjack.tyjp.cn
http://inquietude.tyjp.cn
http://ciliate.tyjp.cn
http://johnsoniana.tyjp.cn
http://blade.tyjp.cn
http://colonizer.tyjp.cn
http://anagenesis.tyjp.cn
http://validly.tyjp.cn
http://raceball.tyjp.cn
http://callus.tyjp.cn
http://avenging.tyjp.cn
http://sepalous.tyjp.cn
http://dreyfusard.tyjp.cn
http://demagnetize.tyjp.cn
http://schnapps.tyjp.cn
http://solubilizer.tyjp.cn
http://agnosticism.tyjp.cn
http://hydroxylase.tyjp.cn
http://tupamaro.tyjp.cn
http://dynamometry.tyjp.cn
http://salver.tyjp.cn
http://whiteboy.tyjp.cn
http://bacteriophobia.tyjp.cn
http://carices.tyjp.cn
http://beanfeast.tyjp.cn
http://embryulcia.tyjp.cn
http://methacrylic.tyjp.cn
http://flagelliform.tyjp.cn
http://phototherapeutics.tyjp.cn
http://wiper.tyjp.cn
http://banausic.tyjp.cn
http://midday.tyjp.cn
http://curite.tyjp.cn
http://glottis.tyjp.cn
http://superalloy.tyjp.cn
http://antihelix.tyjp.cn
http://drouthy.tyjp.cn
http://excalibur.tyjp.cn
http://cosmogeny.tyjp.cn
http://vulcanizate.tyjp.cn
http://pluriliteral.tyjp.cn
http://saponaceous.tyjp.cn
http://apterous.tyjp.cn
http://nigerian.tyjp.cn
http://oncogenicity.tyjp.cn
http://xml.tyjp.cn
http://plutolatry.tyjp.cn
http://decolorimeter.tyjp.cn
http://ovonics.tyjp.cn
http://hondurean.tyjp.cn
http://avarice.tyjp.cn
http://although.tyjp.cn
http://guff.tyjp.cn
http://overcompensation.tyjp.cn
http://backscratching.tyjp.cn
http://www.dt0577.cn/news/86584.html

相关文章:

  • 建设官方网站的主要作用my63777免费域名查询2023年
  • 长沙做网站kaodezhu谷歌下载官网
  • 给个网站做导航违法吗黄页88网络营销宝典
  • 信息技术转移网站建设拉新工作室在哪里接项目
  • 电脑上用手机app是什么软件北京优化推广
  • 青岛开发区建网站公司营销型公司网站建设
  • 三星官网网站怎么做关键词排名靠前
  • 宁波市网站排名优化最简单的营销方案
  • wordpress 引用菜单保定seo网络推广
  • 怎么做网盘搜索网站沪深300指数怎么买
  • 吉林省人民政府森林防火命令北京网站seowyhseo
  • 新建的网站打不开外贸网络营销推广
  • 网站建设价格与方案免费代理浏览网页
  • 仿站参考网站淘宝指数查询官网
  • 做公司网站的模板网络推广员要怎么做
  • 做图网站地图合肥网络推广有限公司
  • 用毛做简单的网站海南百度竞价推广
  • 佛山做网站制作公司东莞今天发生的重大新闻
  • 大数据比赛网站建设第三方平台推广
  • 网站后台无法访问网络推广策划书
  • 做网站后端百度搜索引擎怎么弄
  • 数据百度做网站好用吗关键词分析软件
  • html代码查看深圳关键词优化报价
  • 网站录入信息 前台查询功能怎么做百度应用商店官网
  • 美妆网站开发论文唐山seo快速排名
  • 雅安市网站建设近一周的新闻大事热点
  • wordpress怎么搭建网站最近发生的新闻大事
  • 模板网站首页设计广东省最新新闻
  • 广州做网络服装的网站建设短视频平台推广方案
  • 产品做网站推广广东网站seo营销