詳解F# 2.0中兩大運(yùn)行時(shí)的簡單應(yīng)用
我們知道,F(xiàn)# 2.0 有面向 .NET Framework 2.0 和 .NET Framework 4 的兩個(gè)運(yùn)行時(shí)。此外,F(xiàn)# 2.0 還有一個(gè)面向Silverlight 的運(yùn)行時(shí)。
面向 .NET Framework 2.0 的 F# 2.0 運(yùn)行時(shí)的核心庫 (FSharp.Core.dll) 將一些 API 包含在 System 命名空間中,這些 API 與 F# 開發(fā)所需的 .NET Framework 4 API 相同。例如 System.Numerics.BigInteger 和 System.Tuple 就是這樣。
Windows 操作系統(tǒng):
- E:\> gacutil -l FSharp.Core
- Microsoft (R) .NET Global Assembly Cache Utility. Version 4.0.30319.1
- 版權(quán)所有(C) Microsoft Corporation。保留所有權(quán)利。
- 全局程序集緩存包含下列程序集:
- FSharp.Core, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL
- FSharp.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL
- 項(xiàng)目數(shù) = 2
- E:\> fsi.exe
- Microsoft(R) F# 2.0 Interactive 版本 4.0.30319.1
- 版權(quán)所有(C) Microsoft Corporation。保留所有權(quán)利。
- 若要獲得幫助,請鍵入 #help;;
- > 8I.GetType().AssemblyQualifiedName;;
- val it : string =
- "System.Numerics.BigInteger, System.Numerics, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
- > #quit;;
- E:\> c:
- C:\> cd "\Program Files\Reference Assemblies\Microsoft\FSharp\2.0\Runtime"
- C:\Program Files\Reference Assemblies\Microsoft\FSharp\2.0\Runtime> dir v2.0\FSharp.Core.dll v4.0\FSharp.Core.dll
- 驅(qū)動器 C 中的卷是 OS
- 卷的序列號是 5ACA-EDB5
- C:\Program Files\Reference Assemblies\Microsoft\FSharp\2.0\Runtime\v2.0 的目錄
- 2010/03/19 05:52 938,840 FSharp.Core.dll
- 1 個(gè)文件 938,840 字節(jié)
- C:\Program Files\Reference Assemblies\Microsoft\FSharp\2.0\Runtime\v4.0 的目錄
- 2010/03/19 05:52 895,832 FSharp.Core.dll
- 1 個(gè)文件 895,832 字節(jié)
- 0 個(gè)目錄 4,724,301,824 可用字節(jié)
- C:\Program Files\Reference Assemblies\Microsoft\FSharp\2.0\Runtime>
Linux 操作系統(tǒng):
- ben@ben-1520:~$ gacutil -l FSharp.Core
- The following assemblies are installed into the GAC:
- FSharp.Core, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
- Number of items = 1
- ben@ben-1520:~$ fsi.exe
- Microsoft (R) F# 2.0 Interactive build 2.0.0.0
- Copyright (c) Microsoft Corporation. All Rights Reserved.
- For help type #help;;
- > 8I.GetType().AssemblyQualifiedName;;
- val it : string =
- "System.Numerics.BigInteger, FSharp.Core, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
- > #quit;;
- - Exit...
- ben@ben-1520:~$ cd /usr/local/bin/FSharp-2.0.0.0/bin/ && ls -l FSharp.Core.dll
- -rw-r--r-- 1 root root 938,840 2010-06-14 23:54 FSharp.Core.dll
- ben@ben-1520:/usr/local/bin/FSharp-2.0.0.0/bin$
使用 .NET Reflector 來查看:
讓我們琮看看下面的 test.fs 程序:
- 1: open System;
- 2: printfn "OS Version: %A" Environment.OSVersion
- 3: printfn "CLR Version: %A" Environment.Version
- 4: printfn "%s" (8I.GetType().AssemblyQualifiedName)
在 Linux 操作系統(tǒng)下編譯和運(yùn)行(使用面向 .NET Framework 2.0 的 F# 2.0 運(yùn)行時(shí)):
- ben@ben-1520:~/work$ fsc.exe test.fs
- Microsoft (R) F# 2.0 Compiler build 2.0.0.0
- Copyright (c) Microsoft Corporation. All Rights Reserved.
- ben@ben-1520:~/work$ fsc.exe --out:test2.exe --standalone test.fs
- Microsoft (R) F# 2.0 Compiler build 2.0.0.0
- Copyright (c) Microsoft Corporation. All Rights Reserved.
- ben@ben-1520:~/work$ ls -l test*.exe
- -rwxr-xr-x 1 ben ben 923,136 2010-08-15 14:25 test2.exe
- -rwxr-xr-x 1 ben ben 4,608 2010-08-15 14:25 test.exe
- ben@ben-1520:~/work$ ./test.exe
- OS Version: Unix 2.6.32.24
- CLR Version: 2.0.50727.1433
- System.Numerics.BigInteger, FSharp.Core, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
- ben@ben-1520:~/work$ ./test2.exe
- OS Version: Unix 2.6.32.24
- CLR Version: 2.0.50727.1433
- System.Numerics.BigInteger, test2, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null
- ben@ben-1520:~/work$
上面的 test2.exe 使用 --standalone 參數(shù)編譯,表示以靜態(tài)方式將 F# 庫與依賴于此庫的所有引用的 DLL 鏈接到所生成的程序集中??梢钥闯?,在 test.exe 程序中,System.Numerics.BigInteger 是定義中 FSharp.Core 程序集中,而在 test2.exe 中,System.Numerics.BigInteger 被鏈接到生成的 test2 程序集中。
在 Windows 操作系統(tǒng)下編譯和運(yùn)行(使用面向 .NET Framework 4 的 F# 2.0 運(yùn)行時(shí)):
- E:\work> fsc.exe test.fs
- Microsoft(R) F# 2.0 編譯器版本 4.0.30319.1
- 版權(quán)所有(C) Microsoft Corporation。保留所有權(quán)利。
- E:\work> fsc.exe --out:test2.exe --standalone test.fs
- Microsoft(R) F# 2.0 編譯器版本 4.0.30319.1
- 版權(quán)所有(C) Microsoft Corporation。保留所有權(quán)利。
- E:\work> dir test*.exe
- 驅(qū)動器 E 中的卷是 Data2
- 卷的序列號是 16BB-989E
- E:\work 的目錄
- 2010/08/15 14:29 5,120 test.exe
- 2010/08/15 14:30 880,128 test2.exe
- 2 個(gè)文件 885,248 字節(jié)
- 0 個(gè)目錄 51,641,475,072 可用字節(jié)
- E:\work> test.exe
- OS Version: Microsoft Windows NT 6.0.6002 Service Pack 2
- CLR Version: 4.0.30319.1
- System.Numerics.BigInteger, System.Numerics, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
- E:\work> test2.exe
- OS Version: Microsoft Windows NT 6.0.6002 Service Pack 2
- CLR Version: 4.0.30319.1
- System.Numerics.BigInteger, System.Numerics, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
- E:\work>
可能看出,使用面向 .NET Framework 4 的 F# 2.0 運(yùn)行時(shí),System.Numerics.BigInteger 是 .NET Framework 4 的一部分,位于 System.Numerics 程序集中。不管是否使用 --standalone 參數(shù)編譯,都不會改變這一點(diǎn)。
此外,IronRuby 以及 IornPython 也分成面向 .NET Framework 2.0 和 .NET Framework 4 兩個(gè)版本。
原文標(biāo)題:淺談 F# 2.0 的兩個(gè)運(yùn)行時(shí)
鏈接:http://www.cnblogs.com/skyivben/archive/2010/08/15/1800052.html
【編輯推薦】