C# 基本特性

来源:互联网 发布:status c语言 编辑:程序博客网 时间:2024/06/02 02:49

最近要改C++的东西到C#上,它的委托了特性感觉很好的利用了装饰模式和函数指针的优点,非常值得深入学习,Mark基本特性以便查阅

 1: #define DoTrace
 2: 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_Sharp
 25: {
 26: 
 27: 
 48: 
 49: 
 50: 
 56: [AttributeUsage(AttributeTargets.Method|AttributeTargets.Class,
 57: Inherited = false,
 58: AllowMultiple = false)] //限制属性只能引用在方法和类上,并且特性不会被装饰类的派生类继承,不能有MyAttribute的多个实例应用到同一个目标上
 59: public sealed class MyAttributeAttribute : System.Attribute
 60: {
 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 Class
 72:  
 73: [MyAttribute("This is a Attribute Class test Class", Reviewer = "Lucy Liu", ver = "1.0Beta")]
 74: class attributeTestClass
 75: {
 76: public void writeDescription()
 77: {
 78: Console.WriteLine("-----MyAttribute Class Testing----");
 79: }
 80: }
 81:  
 82:  
 83: 
 84: 
 90: 
 91: public class dllIMportClass
 92: {
 93:  
 94: [DllImportAttribute("CDll_for_DLLImport.dll")]
 95: public static extern int sampleMethod(int i);
 96:  
 97: }//dllImportClass
 98: 
 99: 
 100: 
 120: [Serializable]
 121: public class Book
 122: {
 123: public string strBookName;
 124: [NonSerialized]
 125: public string strBookPwd;
 126:  
 127: private string _bookID;
 128: public string BookID
 129: {
 130: get { return _bookID; }
 131: set { _bookID = value; }
 132: }
 133: 
 134: public ArrayList alBookReader;
 135:  
 136: private String _bookPrice;
 137: public void SetBookPrice(String price)
 138: {
 139: _bookPrice = price;
 140: }
 141:  
 142: public Book()
 143: {
 144: alBookReader = new ArrayList();
 145: }
 146:  
 147: public void Write()
 148: {
 149: Console.WriteLine("Book ID:" + BookID);
 150: Console.WriteLine("Book Name" + strBookName);
 151: Console.WriteLine("Book Password:" + strBookPwd);
 152: Console.WriteLine("Book Price" + _bookPrice);
 153: Console.WriteLine("Book Reader");
 154: for(int i = 0; i
 155: {
 156: Console.WriteLine(alBookReader[i]);
 157: }
 158: }
 159: }
 160:  
 161: public class BinarySerialize
 162: {
 163: string strFile = "c:book.binary";
 164:  
 165: public void Serialize(Book book)
 166: {
 167: using(FileStream fs = new FileStream(strFile, FileMode.Create))
 168: {
 169: BinaryFormatter formatter = new BinaryFormatter();
 170: formatter.Serialize(fs, book);
 171: }
 172: Console.WriteLine("Binary Serialize Success");
 173: }
 174:  
 175: public Book DeSerialize()
 176: {
 177: Book book;
 178: using (FileStream fs = new FileStream(strFile, FileMode.Open))
 179: {
 180: BinaryFormatter formatter = new BinaryFormatter();
 181: book = (Book)formatter.Deserialize(fs);
 182: }
 183: Console.WriteLine("Binary Deseiralize Success");
 184: return book;
 185: }
 186: }//BinarySerialize
 187:  
 188: public class SoapSerialize
 189: {
 190: string strFile = "c:book.soap";
 191:  
 192: public void Serialize(Book book)
 193: {
 194: using(FileStream fs = new FileStream(strFile, FileMode.Create))
 195: {
 196: SoapFormatter formatter = new SoapFormatter();
 197:  
 198: formatter.Serialize(fs, book);
 199:  
 200: Console.WriteLine("Soap Serialize Success");
 201: }
 202: }
 203:  
 204: public Book DeSerialize()
 205: {
 206: Book book;
 207: using(FileStream fs = new FileStream(strFile, FileMode.Open))
 208: {
 209: SoapFormatter formatter = new SoapFormatter();
 210: book = (Book)formatter.Deserialize(fs);
 211: }
 212: Console.WriteLine("Soap Deserialize Success");
 213: return book;
 214: }
 215: }//SerializeSoap
 216:  
 217: public class XmlSerialize
 218: {
 219: string strFile = "c:book.xml";
 220:  
 221: public void Serialize(Book book)
 222: {
 223: using(FileStream fs = new FileStream(strFile, FileMode.Create))
 224: {
 225: XmlSerializer formatter = new XmlSerializer(typeof(Book));
 226: formatter.Serialize(fs, book);
 227:  
 228: Console.WriteLine("XML Serialize Success");
 229: }
 230: }
 231:  
 232: public Book DeSerialize()
 233: {
 234: Book book;
 235: using(FileStream fs= new FileStream(strFile, FileMode.Open))
 236: {
 237: XmlSerializer formatter = new XmlSerializer(typeof(Book));
 238: book = (Book)formatter.Deserialize(fs);
 239: }
 240: Console.WriteLine("XML Deserialize Success");
 241: return book;
 242: }
 243: }//SeiralizeXML
 244:  
 245: class Test_Reflection
 246: {
 247: 
 248: 
 251: 
 252:  
 253: [Obsolete("Use method SuperPrintOut")] //将特性应用到方法
 254: static void PrintOut(string str)
 255: {
 256: Console.WriteLine(str);
 257: }
 258:  
 259: [Conditional("DoTrace")]
 260: static void TraceMessage(string str)
 261: {
 262: Console.WriteLine(str);
 263: }
 264:  
 265: 
 266: 
 271: 
 272: static void Main(string[] args)
 273: {
 274: //Absolete Test
 275: PrintOut("Start of Main");
 276:  
 277: //Conditional Test
 278: TraceMessage("Test Conditional ");
 279: TraceMessage("Test Conditional Again");
 280: 
 281: //Serialize Test
 282: Book book = new Book();
 283: book.BookID ="1";
 284: book.alBookReader.Add("gspring");
 285: book.alBookReader.Add("Lucy");
 286: book.strBookName = "C#Plus";
 287: book.strBookPwd = "*****";
 288: book.SetBookPrice("30.00");
 289:  
 290: BinarySerialize bs = new BinarySerialize();
 291: SoapSerialize ss = new SoapSerialize();
 292: XmlSerialize xs = new XmlSerialize();
 293:  
 294: bs.Serialize(book);
 295: ss.Serialize(book);
 296: xs.Serialize(book);
 297:  
 298: Book rebook = bs.DeSerialize();
 299: rebook.Write();
 300: rebook = ss.DeSerialize();
 301: rebook.Write();
 302: rebook = xs.DeSerialize(); //xml 只能序列化public
 303: rebook.Write();
 304:  
 305: //DllImport Test
 306: Console.WriteLine("DllImport Test, The Return Value is :{0}", dllIMportClass.sampleMethod(5));
 307: 
 308: //MyAttribute Test
 309: attributeTestClass atc = new attributeTestClass();
 310: //可用isDefined方法查看特性是否应用到了某个类上
 311: Type t = atc.GetType();
 312: bool isDefined = t.IsDefined(typeof(MyAttributeAttribute), false);
 313: if (isDefined)
 314: Console.WriteLine("MyAttributeAttribute is applied to {0}", atc.GetType().Name);
 315: //可用GetCustomAttribute方法返回应用到结构上的特性数组,实际返回了Object数组,所以需要将其强制转换为特性类型
 316: object[] attrArr = t.GetCustomAttributes(false);
 317: foreach(Attribute a in attrArr)
 318: {
 319: MyAttributeAttribute attr = a as MyAttributeAttribute;
 320: if( null!=attr )
 321: {
 322: Console.WriteLine("Description:{0}", attr.Description );
 323: Console.WriteLine("Version Number:{0}", attr.ver);
 324: Console.WriteLine("Reviewer ID:{0}", attr.Reviewer);
 325: }
 326: }
 327:  
 328: Console.ReadLine();
 329: }
 330: }
 331: }
原创粉丝点击