最近要改C++的东西到C#上,它的委托了特性感觉很好的利用了装饰模式和函数指针的优点,非常值得深入学习,Mark基本特性以便查阅
1: #define DoTrace2: using System;3: using System.Collections.Generic;4: using System.Linq;5: using System.Text;6: using System.Reflection;7: using System.Diagnostics;8: using System.Collections;9: using System.IO;10: 11: using System.Runtime.Serialization.Formatters.Binary;12: using System.Runtime.Serialization.Formatters.Soap;13: using System.Xml.Serialization;14: using System.Xml;15: using System.Runtime.InteropServices;16: 17:18:
19:
22:
23: 24: namespace First_C_Sharp25: {
26:
27:
48:
49:
50:
56: [AttributeUsage(AttributeTargets.Method|AttributeTargets.Class,
57: Inherited = false,58: AllowMultiple = false)] //限制属性只能引用在方法和类上,并且特性不会被装饰类的派生类继承,不能有MyAttribute的多个实例应用到同一个目标上59: public sealed class MyAttributeAttribute : System.Attribute60: {
61: public string Description;62: public string ver;63: public string Reviewer;64: 65: public MyAttributeAttribute(string desc) //一个位置参数66: {
67: Description = desc;
68: Console.WriteLine("Using Attribute Class:{0}", Description);69: }
70: 71: }//MyAttributeAttribute Class72: 73: [MyAttribute("This is a Attribute Class test Class", Reviewer = "Lucy Liu", ver = "1.0Beta")]74: class attributeTestClass75: {
76: public void writeDescription()77: {
78: Console.WriteLine("-----MyAttribute Class Testing----");79: }
80: }
C# 基本特性
最新推荐文章于 2022-05-12 13:34:59 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)