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

织梦网站地图制作教程十堰高端网站建设

织梦网站地图制作教程,十堰高端网站建设,网站建设模拟实验报告,全国企业年报网上申报C#进阶1 本文章主要介绍C#的进阶知识#xff0c;如反射#xff0c;特性.... 参考视频链接 原码 文章目录 C#进阶1反射步骤泛型反射调用方法 获取属性 特性特性的定义步骤扩展枚举练习 反射 在 C# 中#xff0c;反射#xff08;Reflection#xff09;是一种强大的机制如反射特性.... 参考视频链接 原码 文章目录 C#进阶1反射步骤泛型反射调用方法 获取属性 特性特性的定义步骤扩展枚举练习 反射 在 C# 中反射Reflection是一种强大的机制允许程序在运行时检查和操作类型、方法、属性等元数据。通过反射你可以在运行时动态地创建对象、调用方法、访问属性甚至修改类型的行为。反射在许多场景中非常有用 反射思路首先获取程序中的类然后通过类再获取方法参数构造方法等 在项目路径下的debug文件中.dll是首先要获取的因为他是程序一次编译形成的产物相关信息都可以通过反射获取 步骤 加载dll dll是经过一次编译形成的文件 Assembly assembly Assembly.Load(2024_10_30_Project);获取类 Type type assembly.GetType(_2024_10_30_Project.Person);调用构造创建实例 //创建无参Person p (Person)Activator.CreateInstance(type, true);p.say();//调用有参Object o Activator.CreateInstance(type, new Object[] { 我要说话 });泛型反射 对于类的话如果类中有泛型那么在获取类的时候要制定泛型个数然后说明泛型 //调用泛型无参 3表示三个泛型Type type2 assembly.GetType(_2024_10_30_Project.Zhou3);//说明泛型类型type2 type2.MakeGenericType(new Type[] { typeof(int), typeof(string), typeof(DateTime) });//创建Object o2 Activator.CreateInstance(type2);调用方法 类中获取方法所以得有类先获取实例然后进行方法的获取方法获取要制定获取的方法名如果不是泛型方法在获取方法名的同时指定方法参数类型如果是泛型方法就先获取方法名然后再制定泛型参数最后调用方法。 //方法反射{MethodInfo method3 type2.GetMethod(zMethod3, new Type[] { });method3.Invoke(o2, null);}{var method2 type2.GetMethod(zMethod2, new Type[] { typeof(string), typeof(int) });method2.Invoke(o2, new object[] { 张三, 11 });}{//调用私有MethodInfo method1 type2.GetMethod(zMethod1, BindingFlags.Instance | BindingFlags.NonPublic);method1.Invoke(o2, new object[] { 我是私有m1 });}{//调用泛型共有方法Assembly a1 Assembly.Load(2024_10_30_Project);Type type1 a1.GetType(_2024_10_30_Project.TestClaz1);type1 type1.MakeGenericType(new Type[] { typeof(int) });Object oo1 Activator.CreateInstance(type1);MethodInfo me1 type1.GetMethod(m1);var me2 me1.MakeGenericMethod(new Type[] { typeof(string), typeof(DateTime) });me2.Invoke(oo1, new object[] { 1, 张张, DateTime.Now });}{//调用泛型私有方法Assembly a1 Assembly.Load(2024_10_30_Project);Type type1 a1.GetType(_2024_10_30_Project.TestClaz1);type1 type1.MakeGenericType(new Type[] { typeof(int) });Object oo1 Activator.CreateInstance(type1);MethodInfo me1 type1.GetMethod(m2,BindingFlags.Instance|BindingFlags.NonPublic);var me2 me1.MakeGenericMethod(new Type[] { typeof(string) });me2.Invoke(oo1, new object[] { 1, 私有 });}获取属性 由于获取的方式都参不多大家可自信查阅api {Assembly a1 Assembly.Load(2024_10_30_Project);Type type1 a1.GetType(_2024_10_30_Project.TestClaz1);type1 type1.MakeGenericType(new Type[] { typeof(int) });Object oo1 Activator.CreateInstance(type1);var pr type1.GetProperties();foreach(var pro in pr){Console.WriteLine(pro.Name);if (pro.Name.Equals(age)){pro.SetValue(oo1, 11);//设置属性值}Console.WriteLine(pro.GetValue(oo1));}}特性 特性类似于java中的注解用于给元素添加额外的信息 特性的定义 如需要自定义特性需要继承Attribute使用如果自定义特性包含Attribute可省略 class CustomerAttribute:Attribute { }[Customer] class Person {public static void say(){Console.WriteLine(我是特性说话了);} }步骤 声明自定义特性定义属性和方法 namespace AttriProject {class CustomerAttribute:Attribute{public string name { get; set; }public int age { get; set; }public void say(){Console.WriteLine(我是特性类);}} }然后在Person类上添加特性 //初始化特性给特性赋值[Customer(name 张三,age 11)]class Person{[Customer]public string name { get; set; }public static void say(){Console.WriteLine(我是特性说话了);}}在Manager类声明方法通过反射获取Person类的特性 public static void show(){Type type typeof(Person);//获取类typeif (type.IsDefined(typeof(CustomerAttribute), true)) //看类中是否包含特性{//获取类的特性实例CustomerAttribute customer (CustomerAttribute)type.GetCustomAttribute(typeof(CustomerAttribute));Console.WriteLine(${customer.age}-{customer.name});customer.say();}PropertyInfo proin type.GetProperty(name);if (proin.IsDefined(typeof(CustomerAttribute), true)) //看类中是否包含特性{//获取类的特性实例CustomerAttribute customer (CustomerAttribute)proin.GetCustomAttribute(typeof(CustomerAttribute));customer.say();}}扩展枚举练习 定义自定义特性 class CustomerAttribute:Attribute{private string _remark;public string Remark { get _remark; set _remark value; }public CustomerAttribute(string remark){this._remark remark;} }定义枚举并在字段上标注特性 enum CEnum{[CustomerAttribute(春天)]SPRING1,[CustomerAttribute(夏天)]SUMMER 2,[CustomerAttribute(秋天)]AUTUMN 3,[CustomerAttribute(冬天)]WINTER 4}定义枚举扩展方法扩展方法为静态类中包含静态方法静态方法参数用this标识这样方法参数类型调用此方法会自动进入该方法 扩展方法返回特性的属性值 public static class RemarkExten{public static string GetRemark(this Enum en){var type en.GetType();var field type.GetField(en.ToString());if (field.IsDefined(typeof(CustomerAttribute), true)){var cu (CustomerAttribute)field.GetCustomAttribute(typeof(CustomerAttribute));return cu.Remark;}return ;}}主方法调用 static void Main(string[] args){Manager.show();CEnum c CEnum.SPRING;var x c.GetRemark();Console.WriteLine(x);//春天}
http://www.hkea.cn/news/14289903/

相关文章:

  • 网站建设费用折旧年限网站建设公司推广广告语
  • 企业网站模板做视频在哪个网站收益高
  • 怎么开网站做理论的网站
  • 做电脑网站手机能显示不出来怎么办网页图片转换成word文档
  • 网站开发实现的环境杭州建设项目审批网站
  • app网站制作要多少钱设计服务网站
  • 网站怎么宣传win10系统优化工具
  • 喜欢做网站土地流转网站开发
  • 有没有什么 网站能够做试卷wordpress需要伪静态吗
  • php做网站一般用什么软件网站后台树形菜单样式
  • 网站建设计划书内容专业网页网站设计图书
  • 硬盘做免费嗳暧视频网站网站外链建设与维护
  • 旅游网站建设案例网站建设 套格式
  • 高校网站集群平台子站开发官网在线制作
  • 深圳网站建设南山郑州seo网站有优化
  • 上市公司中 哪家网站做的好做网站公司哪个好
  • 厦门seo网站建设费用外贸公司取名
  • 对网站开发实训的建议软件通网站建设
  • 高质量的网站建设国外云服务器厂商
  • 网站建设文档搜索引擎优化策略应该包括
  • h5网站设计报价如何访问自己做的网站
  • 嘉兴网站制作费用安徽 网站开发
  • 口碑好网站建设电话广州五羊建设官方网站
  • 电子商务网站建设臧良运课后答案南通网站建设兼职
  • 做淘宝电商比较厉害的网站太原网站 制作
  • 深圳做网站的公司哪个好深圳商城网站设计制作
  • 微信房地产网站建设怎么建自己的网站?
  • 自助网站建设公司电话青岛知名网站建设公司排名
  • 汕头免费建站网页模板制作工具
  • 网站的主题是什么2880元网站建设