.Net Compact Framework 基础篇(2)

来源:互联网 发布:html js区别 编辑:程序博客网 时间:2024/04/19 16:33

导读:.Net Compact Framework 基础篇(1)

在上一篇文章中,介绍了几个命名空间的使用,这篇文章,主要叙述System命名空间里的主要类及其方法。

1.AppDomain:
表示应用程序域,它是一个应用程序在其中执行的独立环境。应用程序域为执行托管代码提供隔离、卸载和安全边界。
多个应用程序域可以在一个进程中运行;但是,在应用程序域和线程之间没有一对一的关联。多个线程可以属于一个应用程序域,尽管给定的线程并不局限于一个应用程序域,但在任何给定时间,线程都在一个应用程序域中执行。
在CF中,AppDomain只提供以下几个方法:
通过CreateDomain来创建一个应用程序域。
通过Assembly来加载和执行程序集。
通过Unload来卸载。

代码:

 1            // AppDomain:
 2            // Current Domain. FriendlyName:
 3            this.listBox1.Items.Add("Current Domain. FriendlyName:");
 4            this.listBox1.Items.Add(System.AppDomain.CurrentDomain.FriendlyName);
 5            this.listBox1.Items.Add("");
 6
 7            // Create a domain:
 8            System.AppDomain domain = System.AppDomain.CreateDomain("Test");
 9            this.listBox1.Items.Add("Create a domain.FriendName:");
10            this.listBox1.Items.Add(domain.FriendlyName);
11            this.listBox1.Items.Add("");
12
13            // Domain excute assembly.
14            //domain.ExecuteAssembly(@"SmartDeviceExe.exe");
15
16            // Unload domain
17            //System.AppDomain.Unload(domain);

domain执行ExecuteAssembly则会调用该exe程序
AppDomain.CurrentDomain则返回该线程的Domain

Tips:移动平台不支持将程序集加载到非特定域的代码区域以供多个应用程序域使用。

2.BitConverter
将基本类型转换到byte数组,或将byte数组转换成基本类型。
代码:
大家肯定都很熟悉,在此只介绍2点。
a.可以通过DateTime.UtcNow来获取当前的Utc时间
b.可以通过DateTime.SpecifyKind()来获取某个时间的Utc时间或者Local时间。
有位朋友问我如何得到时区差,可以不用API,通过UtcNow时间与Now时间的差额得到。
代码:


4.GC
垃圾回收,在此不介绍其原理及详细说明,只介绍几个方法使用。
代码:
MaxGetation得到垃圾回收堆里最大的一代,垃圾会有第几代第几代,最初为0代。

5.Delegate
委托,不做说明

 1            // BitConverter:
 2            this.listBox1.Items.Add("BitConverter string to byte:");
 3            byte[] byte1 = BitConverter.GetBytes(2045);
 4            string strInfo = "";
 5            for (int i = 0; i < byte1.Length; i++)
 6            {
 7                strInfo += byte1[i].ToString() + " ";
 8            }

 9            this.listBox1.Items.Add(strInfo);
10            this.listBox1.Items.Add("");
11
12            this.listBox1.Items.Add("BitConverter byte to string:");
13            this.listBox1.Items.Add(BitConverter.ToInt32(byte1, 0).ToString());
14            this.listBox1.Items.Add("");

4.DateTime

 1            // DateTime:
 2            DateTime utcTime = DateTime.UtcNow;
 3            this.listBox1.Items.Add("Utc Time:");
 4            this.listBox1.Items.Add(utcTime.ToString());
 5            this.listBox1.Items.Add("");
 6
 7            DateTime locTime = DateTime.Now;
 8            this.listBox1.Items.Add("Local Time:");
 9            this.listBox1.Items.Add(locTime.ToString());
10            this.listBox1.Items.Add("");
11
12            DateTime spTime = DateTime.SpecifyKind(new DateTime(2008310), DateTimeKind.Utc);
13            this.listBox1.Items.Add("SpecifyKind Time:");
14            this.listBox1.Items.Add(spTime.ToString());
15            this.listBox1.Items.Add("");


3.CharEnumerator
通过对象的GetEnumerator方法来得到char枚举集合。
代码:

// Char Emunerator
            
//strInfo = "appleseeker";
            CharEnumerator charEnum = strInfo.GetEnumerator();
            
this.listBox1.Items.Add("Char Emunerator:");
            
string strCharEnum = "";
            
while (charEnum.MoveNext())
            
{
                strCharEnum 
+= charEnum.Current.ToString() + " ";
            }

            
this.listBox1.Items.Add(strCharEnum);
            
this.listBox1.Items.Add("");

得到CharEnumerator对象后,不调用MoveNext方法,直接使用Current是会报异常的,MoveNext方法,将指针指向第一个字符。如果没有这返回false。

1// GC
2            this.listBox1.Items.Add("GC MaxGeneration:");
3            this.listBox1.Items.Add(GC.MaxGeneration.ToString());
4            this.listBox1.Items.Add("GC GetTotalMemory:");
5            this.listBox1.Items.Add(GC.GetTotalMemory(true).ToString());
6            //GC.KeepAlive(domain);
7            this.listBox1.Items.Add("");
8            GC.Collect();

 

6.StringComparer
字符串比较,比较2个字符串是否相等。可以自己定义比较。
代码:
7.TimeSpan
时间间隔对象,可以从小时、分钟、秒等来得到一个时间间隔。在很多方法中用到该对象。比如:Thread.Sleep(TimeSpan);
代码:


8.TimeZone
时区,可以得到本地时区,及对时间进行转换,转换成UTC时间
代码:


System命名空间中还有以下主要接口:

1            // String Comparer
2            this.listBox1.Items.Add("App Comparer app:");
3            this.listBox1.Items.Add(StringComparer.CurrentCulture.Compare("App""app").ToString());
4            this.listBox1.Items.Add("");

 

1            // Time Span
2            this.listBox1.Items.Add("Time Span:");
3            this.listBox1.Items.Add("1 hours = " + TimeSpan.FromHours(1).TotalSeconds.ToString() + " s");
4            this.listBox1.Items.Add("1 minutes = " + TimeSpan.FromMinutes(1).TotalSeconds.ToString() + " s");
5            this.listBox1.Items.Add("");

TimeSpan.FromHours(1):得到1小时的时间间隔。TotalSeconds方法,取得该时间间隔对应的多少秒。

1            // Time Zone
2            this.listBox1.Items.Add("Time Zone:");
3            this.listBox1.Items.Add(TimeZone.CurrentTimeZone.ToUniversalTime(System.DateTime.Now).ToString());
4            this.listBox1.Items.Add("Current Time Zone Name:");
5            this.listBox1.Items.Add(TimeZone.CurrentTimeZone.StandardName);
6            this.listBox1.Items.Add("");

TimeZone.CurrentZone得到当前市区,StandardName得到当前的时区的名称。

1.ICloneable
实现该接口后,必须重写Clone方法。
深拷贝、浅拷贝很容易混淆不清,可以找下相关资料。

2.IComparable
实现该接口后,必须重写CompareTo方法。
在集合对象排序时,可以通过自定义的CompareTo方法来排序集合。

3.IDisposable
实现该接口后,必须重写Dispose方法。
通常在using代码块中的定义的变量,只生存在该代码块中,一旦结束自动调用Dispose方法。所以using()中声明的对象必须实现了Dispose方法。

该命名空间中还提供以下值类型,并有相应的对应类:
Array(array), Boolean(bool), Byte(byte), Char(char), Decimal(decimal), Double(double), Int16, Int32, Int63, Object(object), SByte(sbyte), Single(single), String(string), UInt16, UInt32, UInt64, Void

最后简述下以下几个类:
1.Console:控制台输出输入,主要方法Write, WriteLine, ReadLine
2.Convert:各类型转换类,举例 Convert.ToXXX(T) 将T类型转换成XXX类型
3.DateTime:时间类,获取当前时间,且提供时间计算等
4.Enum:枚举类,枚举为值类型
5.Math:数学计算类,静态类,提供很多数学计算方法,举例 Math.Max(T1, T2)比较2数的最大值
6.Nullable:表示空类型,比较对象是否为空。
7.Random:随机类,提供一个随机数,可以在一定区域内的,也可以是非负的。

Demo下载:DeviceAPIDemo_2008_03_11.rar

Demo运行效果:

调用Get Info后得到相应信息。

总结:
本文以简单明了的方式介绍System命名空间下的一些类及方法,希望给大家做个参考,或许某些方法可能大家并不常用。
之所以具体到某些方法的应用,是因为。看到某个朋友问我如何取得ppc上的时区,这个虽然可以通过API取得。但是取得的方法并不是只有一种,可以通过多种途径达到目的。API固然强大,但也有不好的地方,尤其在移动方法,能用framework提供的就用。
综上所述,纯属个人意见。

Author:Appleseeker
Date:2008-03-11
 

原创粉丝点击