博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

随笔3

Posted on 2009-03-21 16:43  Aimee  阅读(204)  评论(0)    收藏  举报
随笔3 2009-02-11 16:18 (分类:工作资料)

 

***************************************

2月11
****************************************
.net framework框架的设计
第1章:  .NET Framework2.0简介
第 2 章:公共语言运行库和类型
第 3 章:托管代码的编译和执行
第 4 章:委托和事件
第 5 章:读取和写入文件
第  6 章:集合和泛型
第  7 章:数据的序列化 
第  8 章:GDI+
第 9章:在Microsoft? .NET Framework 2.0中实现加密
第10章: COM 组件与.NET Framework程序集之间的交互操作 
第 11章:使用类型元数据 
第 12章:创建多线程应用程序和应用程序域
第 13章:代码访问安全性
第14 章:监视和调试应用程序
第 15 章:使用服务应用程序和电子邮件消息
第 16 章:创建全球化应用程序
第 17 章:配置和安装程序集

------------------------
托管代码:需要二次编译启动会很慢,但是CLR中的GC有垃圾回收机制。
非托管:

范型:命名空间System.Collections.Generic和System.Collections.ObjectMode
using System.Collections.ArrayList 是非范型,编译时正常,运行时出错
集合类的命名空间:using System.Collections
范型的命名空间:System.Collections.Generic
二维:HashTable
*范型类:看资料
 class Program
    {
        static void Main(string[] args)
        {

            CommonData<String> name = new CommonData<String>();
            name.Value = ".NET Framework";
            CommonData<float> version = new CommonData<float>();
            version.Value = 2.0F;
            Console.WriteLine(name.Value);
            Console.WriteLine(version.Value);
            Console.ReadLine();
        }
        public class CommonData<T>
        {
            private T  _data;
            public T  Value
            {
                get
                {
                    return this._data;
                }
                set
                {
                    this._data = value;
                }
            }
        }
*委托范型的定义

-------------------------------
1 弱类型:编译的时候没有进行类型检查
2 C# 托管代码的编译---〉msil---〉本机代码
3 元数据:反射,远程通讯,类的关系图等中使用,对类的描述,方法的描述,属性的描述,相当于一个提纲概要。QQ具体的实现在服务器端
客户端正是因为有元数据的存在,才得以解释QQ中的程序。
4 视图--〉对象浏览器
5 程序集清单信息可以修改

--------------------------------
Chapter 4 事件和委托
委托:
·使用范围: 跨线程通信,跨网络通信(分部式网络),跨应用程序的通讯,
·类似于C++ 中的指针
委托与C++中的指针的异同:委托是完全基于类型安全的机制,但是指针不安全;
指针有值类型和引用类型,但是委托只有方法指针。
委托就是调用方法的方法----〉指针
1,声明委托 public delegate string test1();可放在类的内部,也可以放在类的外部
要求签名一致
public delegate string CommonOperate(int n1, int n2);
public static string Abtract(int a1, int a2)
2,实例化委托对象(绑定)
3,调用委托
 //声明委托
    public delegate string CommonOperate(int n1, int n2);
    class Program
    {
        //public delegate string test2();
        static void Main(string[] args)
        {
            CommonOperate co = new CommonOperate(Operation.Abtract);
            Console.WriteLine(co(10, 5));
            Console.ReadLine();
        }

    }
    //具体实现的对象
    public class Operation
    {
        public static string Abtract(int a1, int a2)
        {
            return (a1 - a2).ToString();
        }
    }

---------------------------
事件也是代理的一种
event CommonOperate<T1, T2> CommonOperated;

*****************************
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DelegateTest
{
    public delegate void OperateMessage(string message);
      //  public event SendData ShowMessage;
    public interface CommonOperate
    {
         event OperateMessage OperateMessaged;
    }
}

-------------
 using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace DelegateTest
{
    public partial class SecondForm : Form,CommonOperate
    {
        public SecondForm()
        {
            InitializeComponent();
        }

       // public delegate void SendData(string message);
        //public event SendData ShowMessage;
        private void button1_Click(object sender, EventArgs e)
        {
            if (OperateMessaged != null)
            {
                OperateMessaged(this.textBox1.Text);
            }
        }

        #region CommonOperate 成员

        public event OperateMessage OperateMessaged;

        #endregion
    }
}

-------------
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace DelegateTest
{
    public partial class FirstForm : Form
    {
        public FirstForm()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            SecondForm sf = new SecondForm();
            sf.OperateMessaged += new OperateMessage(sf_ShowMessage);
            sf.Show();
        }

        void sf_ShowMessage(string message)
        {
            this.label1.Text = message;
        }
    }
}
*****************************
管理文件系统:
Path、File、FileInfo、Directory、DirectoryInfo、DriveInfo 和 FileSystemWatcher
using : 命名空间;创建using语句
try{
using(StreamReader sr=new StreamReader("TestFile.txt"))
{
 String line;
 while((line=sr.ReadLine()!=null))
 {
  Console.WriteLine(line);
 }
}
}catch{}

using 相当于一个方法,这个方法可以自动的关闭资源。
*****************************
示例代码5章要看
 StringBuilder类似于java中的StringBuffer
连接字符串的时候可以提高,减少垃圾。
using System.IO;
using System.IO.Compression;

namespace ConsoleApplication2
{
    class Program
    {
        public static void GZipStreamDemo()
        {
            try
            {
                FileStream myFileStream;
                GZipStream gZippedStream;
                System.IO.FileInfo myFile;
                string originalXMLFileName = "UserData.xml";
                string compressedXMLFileName = "UserData.gz";
                string decompressedXMLFileName = "UnzippedUserData.xml";
                System.Data.DataSet dsTest = new System.Data.DataSet();

                myFile = new FileInfo(originalXMLFileName);
                Console.WriteLine("The original data file, {0} contains {1} bytes.", myFile.Name, myFile.Length.ToString());

                dsTest.ReadXml(originalXMLFileName);

                myFileStream = new FileStream(compressedXMLFileName, FileMode.Create, FileAccess.Write);
                gZippedStream = new GZipStream(myFileStream, CompressionMode.Compress);
                dsTest.WriteXml(gZippedStream);

                gZippedStream.Close();

                myFile = new FileInfo(compressedXMLFileName);
                Console.WriteLine("The file has been compressed to {0} bytes and is stored in {1}.", myFile.Length.ToString

(), myFile.Name);

                //decompress the file
                myFileStream = new FileStream(compressedXMLFileName, FileMode.Open, FileAccess.Read);
                gZippedStream = new GZipStream(myFileStream, CompressionMode.Decompress);

                dsTest = new DataSet();
                dsTest.ReadXml(gZippedStream);
                dsTest.WriteXml(decompressedXMLFileName);

                myFile = new FileInfo(decompressedXMLFileName);
                Console.WriteLine("The file has been uncompressed and is now {0} bytes and is stored in {1}.",

myFile.Length.ToString(), myFile.Name);
            }
            catch (Exception xcp)
            {
                Console.WriteLine(xcp.ToString());
            }
        }

        static void Main(string[] args)
        {
            GZipStreamDemo();
        }
    }
}

 

 

返回目录
--------------------------------------------------------------------------------

 


使用独立存储类保护流信息

 

// Visual C#

using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Data;
using System.IO;
using System.IO.Compression;
using System.IO.IsolatedStorage;
namespace ConsoleApplication2
{
    class Program
    {
        public static void IsolatedStorageFileDemo()
        {
            string isolatedFileName = "DemoIsolatedFile.txt";
            IsolatedStorageFile isoStore;
            IsolatedStorageFileStream isoStream;
            StreamWriter myWriter;
            StreamReader myReader;
            string lineFromIsolatedFile;

            isoStore = IsolatedStorageFile.GetUserStoreForAssembly();

            isoStream = new IsolatedStorageFileStream(isolatedFileName,
                FileMode.Create, isoStore);

            myWriter = new StreamWriter(isoStream);
            myWriter.WriteLine("Writing to isolated storage files is easy!");
            myWriter.WriteLine("It is also secure and stable.");
            myWriter.Close();

            isoStream = new IsolatedStorageFileStream(isolatedFileName,
    FileMode.Open, isoStore);

            myReader = new StreamReader(isoStream);

            while ((lineFromIsolatedFile = myReader.ReadLine()) != null)
            {
                Console.WriteLine(lineFromIsolatedFile);
            }

            myReader.Close();
        }


        static void Main(string[] args)
        {
            IsolatedStorageFileDemo();
        }
    }
}

 

 

返回目录
--------------------------------------------------------------------------------

 

管理字符串

 

// Visual C#

using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Data;
using System.IO;
using System.IO.Compression;
using System.IO.IsolatedStorage;
namespace ConsoleApplication2
{
    class Program
    {
        private static void StreamReaderWriterDemo()
        {
            StreamReader myReader;
            StreamWriter myWriter;
            string fileToRead = "ReadingAndWritingTextFiles.txt";
            string fileToWrite = "NewTextFile.txt";
            string lineInput;
            string codeNumber;
            int position = 0;
            char[] firstCharacters = new char[2];

            try
            {

                myReader = new StreamReader(fileToRead);
                myWriter = new StreamWriter(fileToWrite);
                position = myReader.Read(firstCharacters, 0, 2);
                codeNumber = firstCharacters[0].ToString() + firstCharacters[1].ToString();

                if (codeNumber == "23" || codeNumber == "56")
                {
                    Console.WriteLine("The code for this file is {0}", codeNumber);
                    while ((lineInput = myReader.ReadLine()) != null)
                    {
                        Console.WriteLine(lineInput);
                        myWriter.WriteLine(lineInput);
                    }

                    myWriter.WriteLine();
                    myWriter.WriteLine("This data was modified at " + DateTime.Now.ToLongTimeString());
                    myReader.Close();
                    myWriter.Close();
                }
                else
                {
                    Console.WriteLine("This program only processes text files that begin with 23 or 56.");
                    Console.WriteLine("Goodbye");
                    return;
                }
            }//End try
            catch (Exception xcp)
            {
                Console.WriteLine("There was an error reading the file.");
                Console.WriteLine(xcp.ToString());
            }
        }


        static void Main(string[] args)
        {
            StreamReaderWriterDemo();
        }
    }
}

 


 

返回目录
--------------------------------------------------------------------------------

 


使用BinaryReader和BinaryWriter管理二进制数据

 

// Visual C#

using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Data;
using System.IO;
using System.IO.Compression;
using System.IO.IsolatedStorage;
namespace ConsoleApplication2
{
    class Program
    {
        private static void BinaryReaderWriterDemo()
        {
            const string FileName = "SampleDates.data";
            FileStream myStream;
            BinaryWriter myWriter;
            BinaryReader myReader;
            DateTime theDate;
            Int64 dateInTicks;

            myStream = new FileStream(FileName, FileMode.Create);

            myWriter = new BinaryWriter(myStream);

            theDate = DateTime.Now;
            do
            {
                myWriter.Write(theDate.ToUniversalTime().Ticks);
                theDate = theDate.AddDays(1);
            } while (theDate < DateTime.Now.AddDays(7));

            myWriter.Close();
            myStream.Close();

            myStream = new FileStream(FileName, FileMode.Open, FileAccess.Read);
            myReader = new BinaryReader(myStream);
            for (int i = 0; i < 7; i++)
            {
                dateInTicks = (myReader.ReadInt64());
                theDate = new DateTime(dateInTicks);
                Console.WriteLine("{0} ticks is {1}", dateInTicks, theDate.ToLongTimeString());
                Console.WriteLine(theDate.ToLongDateString());
            }
            myReader.Close();
            myStream.Close();
        }

 

        static void Main(string[] args)
        {
            BinaryReaderWriterDemo();
        }
    }
}

 

返回目录
--------------------------------------------------------------------------------

 

使用StringBuilder类高效操作字符串

 

// Visual C#

using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Data;
using System.IO;
namespace ConsoleApplication2
{
    class Program
    {
        private static void StringBuilderTest()
        {
            Int32 numberOfLoops = 10000;
            int aproxLengthOfNewString;
            StringBuilder myBuilder = new StringBuilder();
            DateTime theDate;
            DateTime endDate = DateTime.Now.AddDays(numberOfLoops);
            string testString = "";
            DateTime startTime;
            TimeSpan interval;

            theDate = DateTime.Now;
            startTime = DateTime.Now;

            aproxLengthOfNewString = 11 * numberOfLoops;
            myBuilder.Capacity = aproxLengthOfNewString;

            while (theDate < endDate)
            {
                myBuilder.AppendLine(theDate.ToShortDateString());
                theDate = theDate.AddDays(1);
            }
            myBuilder.Replace("/", "-");
            testString = myBuilder.ToString();

            interval = TimeSpan.FromTicks(DateTime.Now.Ticks - startTime.Ticks);

            Console.WriteLine("StringBuilder: It took {0} milliseconds to append {1} strings to create a string of {2}

characters.",

            interval.TotalMilliseconds, numberOfLoops, testString.Length);
            Console.WriteLine();

            testString = "";
            theDate = DateTime.Now;
            startTime = DateTime.Now;

            while (theDate < endDate)
            {
                testString += theDate.ToShortDateString() + "\n";
                theDate = theDate.AddDays(1);
            }
            testString.Replace("/", "-");
            interval = TimeSpan.FromTicks(DateTime.Now.Ticks - startTime.Ticks);
            Console.WriteLine("String: It took {0} milliseconds to append {1} strings to create a string of {2} characters.",
            interval.TotalMilliseconds, numberOfLoops, testString.Length);
        }

 


        static void Main(string[] args)
        {
            StringBuilderTest();
        }
    }
}


 

返回目录
--------------------------------------------------------------------------------

 

MatchCollection类

 

// Visual C#

using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Data;
using System.IO;
namespace ConsoleApplication2
{
    class Program
    {
        private static void StringBuilderTest()
        {
            Int32 numberOfLoops = 10000;
            int aproxLengthOfNewString;
            StringBuilder myBuilder = new StringBuilder();
            DateTime theDate;
            DateTime endDate = DateTime.Now.AddDays(numberOfLoops);
            string testString = "";
            DateTime startTime;
            TimeSpan interval;

            theDate = DateTime.Now;
            startTime = DateTime.Now;

            aproxLengthOfNewString = 11 * numberOfLoops;
            myBuilder.Capacity = aproxLengthOfNewString;

            while (theDate < endDate)
            {
                myBuilder.AppendLine(theDate.ToShortDateString());
                theDate = theDate.AddDays(1);
            }
            myBuilder.Replace("/", "-");
            testString = myBuilder.ToString();

            interval = TimeSpan.FromTicks(DateTime.Now.Ticks - startTime.Ticks);

            Console.WriteLine("StringBuilder: It took {0} milliseconds to append {1} strings to create a string of {2}

characters.",

            interval.TotalMilliseconds, numberOfLoops, testString.Length);
            Console.WriteLine();

            testString = "";
            theDate = DateTime.Now;
            startTime = DateTime.Now;

            while (theDate < endDate)
            {
                testString += theDate.ToShortDateString() + "\n";
                theDate = theDate.AddDays(1);
            }
            testString.Replace("/", "-");
            interval = TimeSpan.FromTicks(DateTime.Now.Ticks - startTime.Ticks);
            Console.WriteLine("String: It took {0} milliseconds to append {1} strings to create a string of {2} characters.",
            interval.TotalMilliseconds, numberOfLoops, testString.Length);
        }

 


        static void Main(string[] args)
        {
            StringBuilderTest();
        }
    }
}