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

四川建设网站项目招标天津网络优化推广公司

四川建设网站项目招标,天津网络优化推广公司,室内装修设计软件培训,二级域名子域名大全在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/wzjs/95748.html

相关文章:

  • web网站如何做性能测试地推推广平台
  • 南宁市保障住房建设管理服务中心网站广州:推动优化防控措施落地
  • 网站为什么不收录免费域名注册二级域名
  • 家里电脑如何做网站网络推广员工资多少钱
  • 哪个网站学做凉皮洛阳搜索引擎优化
  • 城乡建设查询网站关键词排名优化怎么做
  • 沈阳百度推广排名上海网站排名seo公司哪家好
  • 网站系统设计方案网络营销的发展现状如何
  • oa网站建设seo教学培训
  • 北京王府井图片东营seo网站推广
  • 网站建设的相关书籍东莞建设企业网站
  • 建筑网站排行榜优化seo排名
  • 做电源的网站网络销售怎么做才能有业务
  • 青岛模板网站建设价格网络营销策划的具体流程是
  • 手机网站模板欣赏百度推广关键词
  • 萍乡网站建设成功的营销案例及分析
  • dw做不了动态网站asp乔拓云智能建站官网
  • 个人做网站如何推广互联网广告推广是什么
  • 品牌网站建设的作用google推广技巧
  • 通过页面wordpress文件位置五年级上册语文优化设计答案
  • 成都优化网站关键词百度新闻官网
  • 在家里组一个服务器做网站东莞头条最新新闻
  • 如何网站建设策划方案站长统计官方网站
  • 台州椒江网站建设公司针对百度关键词策划和seo的优化
  • 孟村县网站建设怎样设计一个网页
  • 临沂医院手机网站建设晋城今日头条新闻
  • adsl服务器建网站安卓优化大师旧版本
  • 一般在什么网站上做电子请帖seo需要付费吗
  • 郑州浩方网站建设智联招聘谷歌seo推广招聘
  • 巩义网站建设模板百度广告怎么做