详解F# 2.0中两大运行时的简单应用

开发 后端
F# 2.0中有包括.NET Framework 2.0 和 .NET Framework 4 的两个运行时,还有一个面向Silverlight 的运行时。这几大进行时如何应用?是本文的重点。

我们知道,F# 2.0 有面向 .NET Framework 2.0 和 .NET Framework 4 的两个运行时。此外,F# 2.0 还有一个面向Silverlight 的运行时。

面向 .NET Framework 2.0 的 F# 2.0 运行时的核心库 (FSharp.Core.dll) 将一些 API 包含在 System 命名空间中,这些 API 与 F# 开发所需的 .NET Framework 4 API 相同。例如 System.Numerics.BigInteger 和 System.Tuple 就是这样。

Windows 操作系统:

  1. E:\> gacutil -l FSharp.Core  
  2. Microsoft (R) .NET Global Assembly Cache Utility.  Version 4.0.30319.1  
  3. 版权所有(C) Microsoft Corporation。保留所有权利。  
  4.  
  5. 全局程序集缓存包含下列程序集:  
  6.   FSharp.Core, Version=2.0.0.0, Culture=neutralPublicKeyToken=b03f5f7f11d50a3aprocessorArchitecture=MSIL 
  7.   FSharp.Core, Version=4.0.0.0, Culture=neutralPublicKeyToken=b03f5f7f11d50a3aprocessorArchitecture=MSIL 
  8.  
  9. 项目数 = 2  
  10.  
  11. E:\> fsi.exe  
  12.  
  13. Microsoft(R) F# 2.0 Interactive 版本 4.0.30319.1  
  14. 版权所有(C) Microsoft Corporation。保留所有权利。  
  15.  
  16. 若要获得帮助,请键入 #help;;  
  17.  
  18. > 8I.GetType().AssemblyQualifiedName;;  
  19. val it : string =  
  20.   "System.Numerics.BigInteger, System.Numerics, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" 
  21. > #quit;;  
  22.  
  23. E:\> c:  
  24. C:\> cd "\Program Files\Reference Assemblies\Microsoft\FSharp\2.0\Runtime"  
  25. C:\Program Files\Reference Assemblies\Microsoft\FSharp\2.0\Runtime> dir v2.0\FSharp.Core.dll v4.0\FSharp.Core.dll  
  26.  驱动器 C 中的卷是 OS  
  27.  卷的序列号是 5ACA-EDB5  
  28.  
  29.  C:\Program Files\Reference Assemblies\Microsoft\FSharp\2.0\Runtime\v2.0 的目录  
  30.  
  31. 2010/03/19  05:52           938,840 FSharp.Core.dll  
  32.                1 个文件        938,840 字节  
  33.  
  34.  C:\Program Files\Reference Assemblies\Microsoft\FSharp\2.0\Runtime\v4.0 的目录  
  35.  
  36. 2010/03/19  05:52           895,832 FSharp.Core.dll  
  37.                1 个文件        895,832 字节  
  38.                0 个目录  4,724,301,824 可用字节  
  39.  
  40. C:\Program Files\Reference Assemblies\Microsoft\FSharp\2.0\Runtime> 

Linux 操作系统:

  1. ben@ben-1520:~$ gacutil -l FSharp.Core  
  2. The following assemblies are installed into the GAC:  
  3. FSharp.Core, Version=2.0.0.0, Culture=neutralPublicKeyToken=b03f5f7f11d50a3a 
  4. Number of items = 1 
  5. ben@ben-1520:~$ fsi.exe  
  6.  
  7. Microsoft (R) F# 2.0 Interactive build 2.0.0.0  
  8. Copyright (c) Microsoft Corporation. All Rights Reserved.  
  9.  
  10. For help type #help;;  
  11.  
  12. > 8I.GetType().AssemblyQualifiedName;;  
  13. val it : string =  
  14.   "System.Numerics.BigInteger, FSharp.Core, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" 
  15. > #quit;;  
  16.  
  17. - Exit...  
  18. ben@ben-1520:~$ cd /usr/local/bin/FSharp-2.0.0.0/bin/ && ls -l FSharp.Core.dll  
  19. -rw-r--r-- 1 root root 938,840 2010-06-14 23:54 FSharp.Core.dll  
  20. ben@ben-1520:/usr/local/bin/FSharp-2.0.0.0/bin$  

使用 .NET Reflector 来查看:

查看F#

 

让我们琮看看下面的 test.fs 程序:

  1. 1:  open System;  
  2. 2:  printfn "OS  Version: %A" Environment.OSVersion  
  3. 3:  printfn "CLR Version: %A" Environment.Version  
  4. 4:  printfn "%s" (8I.GetType().AssemblyQualifiedName) 

在 Linux 操作系统下编译和运行(使用面向 .NET Framework 2.0 的 F# 2.0 运行时):

  1. ben@ben-1520:~/work$ fsc.exe test.fs  
  2. Microsoft (R) F# 2.0 Compiler build 2.0.0.0  
  3. Copyright (c) Microsoft Corporation. All Rights Reserved.  
  4. ben@ben-1520:~/work$ fsc.exe --out:test2.exe --standalone test.fs  
  5. Microsoft (R) F# 2.0 Compiler build 2.0.0.0  
  6. Copyright (c) Microsoft Corporation. All Rights Reserved.  
  7. ben@ben-1520:~/work$ ls -l test*.exe  
  8. -rwxr-xr-x 1 ben ben 923,136 2010-08-15 14:25 test2.exe  
  9. -rwxr-xr-x 1 ben ben   4,608 2010-08-15 14:25 test.exe  
  10. ben@ben-1520:~/work$ ./test.exe  
  11. OS  Version: Unix 2.6.32.24  
  12. CLR Version: 2.0.50727.1433  
  13. System.Numerics.BigInteger, FSharp.Core, Version=2.0.0.0, Culture=neutralPublicKeyToken=b03f5f7f11d50a3a 
  14. ben@ben-1520:~/work$ ./test2.exe  
  15. OS  Version: Unix 2.6.32.24  
  16. CLR Version: 2.0.50727.1433  
  17. System.Numerics.BigInteger, test2, Version=0.0.0.0, Culture=neutralPublicKeyToken=null 
  18. ben@ben-1520:~/work$  

上面的 test2.exe 使用 --standalone 参数编译,表示以静态方式将 F# 库与依赖于此库的所有引用的 DLL 链接到所生成的程序集中。可以看出,在 test.exe 程序中,System.Numerics.BigInteger 是定义中 FSharp.Core 程序集中,而在 test2.exe 中,System.Numerics.BigInteger 被链接到生成的 test2 程序集中。

在 Windows 操作系统下编译和运行(使用面向 .NET Framework 4 的 F# 2.0 运行时):

  1. E:\work> fsc.exe test.fs  
  2. Microsoft(R) F# 2.0 编译器版本 4.0.30319.1  
  3. 版权所有(C) Microsoft Corporation。保留所有权利。  
  4.  
  5. E:\work> fsc.exe --out:test2.exe --standalone test.fs  
  6. Microsoft(R) F# 2.0 编译器版本 4.0.30319.1  
  7. 版权所有(C) Microsoft Corporation。保留所有权利。  
  8.  
  9. E:\work> dir test*.exe  
  10.  驱动器 E 中的卷是 Data2  
  11.  卷的序列号是 16BB-989E  
  12.  
  13.  E:\work 的目录  
  14.  
  15. 2010/08/15  14:29             5,120 test.exe  
  16. 2010/08/15  14:30           880,128 test2.exe  
  17.                2 个文件        885,248 字节  
  18.                0 个目录 51,641,475,072 可用字节  
  19.  
  20. E:\work> test.exe  
  21. OS  Version: Microsoft Windows NT 6.0.6002 Service Pack 2  
  22. CLR Version: 4.0.30319.1  
  23. System.Numerics.BigInteger, System.Numerics, Version=4.0.0.0, Culture=neutralPublicKeyToken=b77a5c561934e089 
  24.  
  25. E:\work> test2.exe  
  26. OS  Version: Microsoft Windows NT 6.0.6002 Service Pack 2  
  27. CLR Version: 4.0.30319.1  
  28. System.Numerics.BigInteger, System.Numerics, Version=4.0.0.0, Culture=neutralPublicKeyToken=b77a5c561934e089 
  29.  
  30. E:\work> 

可能看出,使用面向 .NET Framework 4 的 F# 2.0 运行时,System.Numerics.BigInteger 是 .NET Framework 4 的一部分,位于 System.Numerics 程序集中。不管是否使用  --standalone 参数编译,都不会改变这一点。

此外,IronRuby 以及 IornPython 也分成面向 .NET Framework 2.0 和 .NET Framework 4 两个版本。

原文标题:浅谈 F# 2.0 的两个运行时

链接:http://www.cnblogs.com/skyivben/archive/2010/08/15/1800052.html

【编辑推荐】

  1. F#中的异步及并行模式:反馈进度的事件
  2. F#中的异步及并行模式:代理的高级使用
  3. F#简明教程三:F#语法精要
  4. F#简明教程二:F#类型系统和类型推断机制
  5. 详细介绍Visual Studio 2010F#使用
责任编辑:彭凡 来源: 博客园
相关推荐

2015-07-20 15:44:46

Swift框架MJExtension反射

2011-05-24 11:03:25

虚拟化

2024-03-21 09:15:58

JS运行的JavaScrip

2009-11-16 09:05:46

CodeTimer

2022-03-01 18:23:17

架构大数据系统

2020-12-07 13:31:43

GoMutex开发者

2010-03-16 09:09:04

F#

2010-07-26 08:46:21

PHP负载均衡

2021-09-11 15:38:23

容器运行镜像开放

2019-07-12 09:30:12

DashboardDockerDNS

2010-04-07 09:46:05

2010-09-14 17:27:12

DIV CSS定位

2018-10-25 09:04:56

Java虚拟机JVM

2010-03-26 19:22:08

F#代理

2010-01-26 08:25:06

F#语法F#教程

2023-08-27 21:07:02

2023-08-21 09:37:57

MySQL工具MariaDB

2024-01-29 08:07:42

FlinkYARN架构

2010-04-01 09:34:06

Oracle函数

2013-11-26 16:49:55

Android开发运行时KitKat
点赞
收藏

51CTO技术栈公众号