C# 基本特性
来源:互联网 发布:status c语言 编辑:程序博客网 时间:2024/06/02 02:49
最近要改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: }
81: 82:83:
84:
90:
91: public class dllIMportClass92: {
93: 94: [DllImportAttribute("CDll_for_DLLImport.dll")]95: public static extern int sampleMethod(int i);96: 97: }//dllImportClass98:
99:
100:
120: [Serializable]
121: public class Book122: {
123: public string strBookName;124: [NonSerialized]
125: public string strBookPwd;126: 127: private string _bookID;128: public string BookID129: {
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; i155: {
156: Console.WriteLine(alBookReader[i]);
157: }
158: }
159: }
160: 161: public class BinarySerialize162: {
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: }//BinarySerialize187: 188: public class SoapSerialize189: {
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: }//SerializeSoap216: 217: public class XmlSerialize218: {
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: }//SeiralizeXML244: 245: class Test_Reflection246: {
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 Test275: PrintOut("Start of Main");276: 277: //Conditional Test278: TraceMessage("Test Conditional ");279: TraceMessage("Test Conditional Again");280:
281: //Serialize Test282: 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 只能序列化public303: rebook.Write();
304: 305: //DllImport Test306: Console.WriteLine("DllImport Test, The Return Value is :{0}", dllIMportClass.sampleMethod(5));307:
308: //MyAttribute Test309: 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: }
阅读全文
0 0
- C# 基本特性
- C++ STL Map基本函数
- 【C#】特性的基本操作
- C# 特性学习(二)
- C# 特性的学习(一)
- 2015 Objective-C 三大新特性
- 【C语言】03 基本数据类型
- 13-基本的C语言特性
- 重温C语言 - 基本特性的实现
- C/C++中const的基本特性
- JAVA Equals 特性
- 【C语言】05 基本语句和运算
- obj-c 开发相关基本名词解释…
- C++STL容器的基本特性和原理
- OC基础10:基本的C语言特性1
- OC基础11:基本的C语言特性2
- C++STL容器的基本特性和原理
- C++ static_cast
- Matching Calibrated Ca…
- 【转】彻底解决Android SDK&n…
- windows窗口相关
- Unity3D协同程序
- 在matlab 画箭头
- C# 基本特性
- 【风宇冲】Unity3D教程宝典…
- .让用户输入一个日期格式如“2008/08/08”,将 输入的日期格式转换为“2008年-8月-8日”。
- [CODE]百度代码规范读后总结
- 图数据库Titan的使用总结
- 《将博客搬至CSDN》
- jmeter和集合点和关联(正则表达式)
- JavaSe 网咯编程
- Ending Learning for Day 5