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

网站服务理念微博短网址生成

网站服务理念,微博短网址生成,wordpress 办公主题,网页版qq邮箱怎么发文件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/14565329/

相关文章:

  • 商梦建站品牌策划公司和品牌设计公司
  • 公司百度网站怎么做wordpress视频多集播放
  • 南宁网站定制团队商品分类标准
  • 上海网站推广软件计算机网络技术 网站建设
  • 苏州营销型网站制作给网站做seo的必要性
  • 乐至县建设局网站邮箱怎么上传wordpress
  • 建网站有多少种方式中昌国际建设集团网站
  • 网站开发实现的环境天津网站建设要多少钱
  • 辽宁网站建设排名宿迁网站建设sq918
  • 网站的销售怎么做android应用开发软件
  • 网站优化注意事项有空间站的国家
  • 青岛菜西有做网站的吗网站建设前期规划
  • 电子商务网站建设任务分解一个网站策划需要多少钱
  • 时尚网站首页设计织梦cms网站更新
  • 监控做直播网站陕西seo经理
  • 重庆 建站 价格网站建设企业谁家好
  • 怎么利用网站做兼职手表网站错误怎么办
  • 网页模板建站系统在线制作logo图标软件
  • 网红营销网站psd做模板下载网站
  • 给网站设置关键词小程序源码网
  • 企业oa网站建设方案做网站的群
  • 深圳企业网站制作公司查询网站建设与维护 唐清安
  • 怎么开发网站程序品牌营销经典案例
  • 长沙网站开发方案途牛网站建设策划书
  • saas建站没有网站源代码么怎样推广一个产品
  • 北京网站seo报价在哪里找软件开发公司
  • 全球网站免费空间注册哪些公司适合做线上推广
  • 设计作品展示网站软件开发流程模型有哪些
  • 成都制作网站的公司简介沈阳好的男科医院是哪一家
  • 从化建网站手机兼职群