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

宿州建设企业网站公司做网站的有哪些学校

宿州建设企业网站公司,做网站的有哪些学校,凤凰军事新闻最新消息,wordpress二次开发版在C#中,特性(Attribute)是一种用于向代码元素(如类、方法、属性等)添加元数据的机制。特性本身不会直接影响代码的执行,但它们可以提供额外的信息,这些信息可以在运行时通过反射(Ref…

        在C#中,特性(Attribute)是一种用于向代码元素(如类、方法、属性等)添加元数据的机制。特性本身不会直接影响代码的执行,但它们可以提供额外的信息,这些信息可以在运行时通过反射(Reflection)来读取和使用。

用法:

一、描述

using System;[AttributeUsage(AttributeTargets.Class, AllowMultiple = true)]
public class DescriptionAttribute : Attribute
{public string Description { get; set; }public DescriptionAttribute(string description){Description = description;}
}
[DescriptionAttribute("描述特性") ]//Attribute允许省略
public class Test
{}public class Program
{public static void Main(string[] args){Type type= typeof(Test);object[] attributes = type.GetCustomAttributes(typeof(DescriptionAttribute), false);if(attributes.Length > 0){DescriptionAttribute descriptionAttribute= (DescriptionAttribute)attributes[0];Console.WriteLine(descriptionAttribute.Description);//会输出文字:描述特性}Console.Read();}
}

二、标记过时方法

using System;public class Program
{[Obsolete("该方法已过时")]public static void ObsoleteMethod(){}public static void Main(string[] args){ObsoleteMethod();Console.Read();}
}

三、控制序列化

using System;
using System.IO;
using System.Xml.Serialization;[Serializable]
public class Test
{public string field1 = "";[XmlIgnore]public string field2 = "";
}public class Program
{public static void Main(string[] args){Test test;Test testDeserializer;XmlSerializer serializer;StringWriter writer;StringReader reader;test = new Test();test.field1 = "field1";test.field2 = "field2";serializer = new XmlSerializer(typeof(Test));writer = new StringWriter(); serializer.Serialize(writer, test);Console.WriteLine($"XML序列化结果:\n{writer}");reader = new StringReader(writer.ToString());testDeserializer = (Test)serializer.Deserialize(reader);Console.WriteLine($"XML反序列化结果:\nfield1:[{testDeserializer.field1}].field2:[{testDeserializer.field2}]" );Console.Read();}
}

四、自定义验证

using System;
using System.Reflection;[AttributeUsage(AttributeTargets.Property)]
public class RangeAttribute : Attribute
{public int Min { get; }public int Max { get; } public RangeAttribute(int min, int max){Min = min;Max = max;}   
}public class Test
{[Range(114,514)]public int Property1 {  get; set; }public  Test(int property1){Property1 = property1;}public bool ValidateProperty1(){MemberInfo property1 = typeof(Test).GetProperty("Property1");RangeAttribute rangeAttribute = (RangeAttribute)Attribute.GetCustomAttribute(property1, typeof(RangeAttribute));if (rangeAttribute != null){return Property1 >= rangeAttribute.Min && Property1 <= rangeAttribute.Max;}else{return true;}}
}public class Program
{public static void Main(string[] args){Test test1 = new Test(123);Test test2 = new Test(999);Console.WriteLine(test1.ValidateProperty1());Console.WriteLine(test2.ValidateProperty1());Console.Read();}
}

五、条件编译

#define Debug
using System;
using System.Diagnostics;public class Program
{[Conditional("Debug")]public static void Log(string message){Console.WriteLine(message);}public static void Main(string[] args){Log("Debug已定义");Console.Read();}
}

注释掉#define Debug代码,则执行不会输出“Debug已定义”。

http://www.dtcms.com/a/574029.html

相关文章:

  • 网络通信的奥秘:HTTP详解 (七)
  • 福建网站建设科技有限公司那些网站做的非常好看的
  • 网站建设老李教学网站商务贸易网站建设
  • 11.5 脚本 本地网站收藏(解封归来)
  • Web APIs 学习第四天:DOM事件进阶
  • 后台网站设计织梦 蓝色 个人网站博客网站源码
  • 余姚网站建设公司wordpress 支付宝免签
  • 仓颉编程语言基础集合类型详解:从Array到HashMap的实战指南(一)
  • 网站负责人姓名wordpress 阿里百秀主题
  • 【C++学习】deque容器
  • 网站程序代码更加精准高效的措施
  • 宁波网站建设h5哪个做网站平台好
  • Apache IoTDB 技术深度解析:存储引擎、查询优化与分布式架构在工业物联网、智慧能源和车联网场景的应用指南
  • rap2部署
  • IOT-VIP——STM32F031K6U6+RS485+显示屏
  • 工业互联网 “云 - 边 - 端” 架构
  • c#网站开发框架深圳网站建设收费
  • MAC电脑上拉取Git仓库的方法
  • 怎样在网做旅游网站顺企网浙江网站建设
  • 时序数据库系列(四):InfluxQL查询语言详解
  • 如何建设专业化的网站全球搜索网站排名
  • Android WebView - loadUrl 方法的长度限制
  • 温州网站推广公司wordpress万能
  • 开题报告之基于SpringBoot框架的在线答题网站的设计与实现
  • 珠海建站专业做辅助的网站
  • 南山做网站海外购物app排行榜前十名
  • 从一题了解 CROSS JOIN 与 DATEDIFF:SQL 天气温度对比实战
  • js:requestAnimationFrame的使用
  • 【JUnit实战3_29】第十八章:REST API 接口测试(上)——RESTful 风格的后端 API 的搭建
  • 回调函数的作用与举例(Python版)