首页 > 代码库 > 《powershell 的版本所引起的加载 FSharp 编译器问题》基本解决

《powershell 的版本所引起的加载 FSharp 编译器问题》基本解决

《powershell 的版本所引起的加载 FSharp 编译器问题》基本解决


1、FSharp.Core.dll,不光要 Add-Type,还要在编译中引用。但是,在 VS2012 的环境下,就不需要引用,百思不得其解;

2、理顺了大部分系统引用的地址。


Function Compiler-FSharp([string]$fSharpCode,[Array]$References)
{
    # 判断 Powershell的版本
    $isAMD64= ($env:PROCESSOR_ARCHITECTURE).Contains("AMD64" )
    $is32= $PSHOME.Contains("SysWOW64")
    $ra= New-Object Collections.ArrayList

    # 增加 Path for FSC.exe
    $FscPath= @("C:\ProgramFiles\Microsoft SDKs\F#\2.0\Framework\v4.0";
                 "C:\ProgramFiles\Microsoft SDKs\F#\3.0\Framework\v4.0";
                 "C:\ProgramFiles\Microsoft SDKs\F#\3.1\Framework\v4.0";
                 "C:\ProgramFiles\Microsoft SDKs\F#\4.0\Framework\v4.0";
                 "C:\ProgramFiles (x86)\Microsoft SDKs\F#\3.0\Framework\v4.0";
                 "C:\ProgramFiles (x86)\Microsoft SDKs\F#\3.0\Framework\v4.0";
                 "C:\ProgramFiles (x86)\Microsoft SDKs\F#\3.1\Framework\v4.0";
                 "C:\ProgramFiles (x86)\Microsoft SDKs\F#\4.0\Framework\v4.0";
                 )
    $FscPath| % {if (Test-Path$_) { $env:Path = $env:Path+ ";" + $_  } }

    if ($isAMD64)
    {
        $FSharpCorePath= @(
            "C:\ProgramFiles (x86)\ReferenceAssemblies\Microsoft\FSharp\.NETFramework\v4.0\4.3.0.0\FSharp.Core.dll"
        )

        if ($is32)
        {
           $mscorlibPath= @(
                "C:\Windows\Microsoft.NET\Framework\v4.0.30319\mscorlib.dll"
            )
        }
        else
        {
            $mscorlibPath= @(
                "C:\ProgramFiles (x86)\ReferenceAssemblies\Microsoft\Framework\.NETFramework\v4.5\mscorlib.dll"
            )
        }
    }
    else
    {
        if ($is32)
        {
            $FSharpCorePath= @(
                "C:\ProgramFiles (x86)\ReferenceAssemblies\Microsoft\FSharp\.NETFramework\v4.0\4.3.0.0\FSharp.Core.dll"
                )
            $mscorlibPath= @(
                "C:\ProgramFiles (x86)\ReferenceAssemblies\Microsoft\Framework\.NETFramework\v4.5\mscorlib.dll";
                "C:\ProgramFiles (x86)\ReferenceAssemblies\Microsoft\Framework\.NETFramework\v3.5\Profile\Client5\mscorlib.dll"

            )

        }
        else
        {
            $FSharpCorePath= @(
                "C:\ProgramFiles\ReferenceAssemblies\Microsoft\FSharp\3.0\Runtime\v4.0\FSharp.Core.dll";
                "C:\ProgramFiles\Reference Assemblies\Microsoft\FSharp\.NETFramework\v4.0\4.3.0.0\FSharp.Core.dll";
                )

            $mscorlibPath= @(           
                "C:\ProgramFiles\ReferenceAssemblies\Microsoft\Framework\.NETFramework\v3.0\mscorlib.dll"
                "C:\ProgramFiles\ReferenceAssemblies\Microsoft\Framework\.NETFramework\v3.5\mscorlib.dll"
                "C:\ProgramFiles\ReferenceAssemblies\Microsoft\Framework\.NETFramework\v4.0\mscorlib.dll"
                "C:\ProgramFiles\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5\mscorlib.dll"
            )
       }
    }


    $FSharpCorePath| % { if(Test-Path($_ )) { Add-Type -Path$_ } }
    $ra.AddRange( @($mscorlibPath  | ? { Test-Path($_ ) } | Select-Object -First1) )
    $ra.AddRange( @($FSharpCorePath | ? {Test-Path($_) } | Select-Object -First1 ) )


    if ($references.Count-ge1)
    {
        $ra.AddRange($References)
        $references|  % { if(Test-Path($_ )) { Add-Type -Path$_} else{Add-Type -AssemblyName$_} }
    }
    # 加载 FSharp.Compiler.CodeDom.dll
    Add-Type -Path C:\FSharpPowerPack-4.0.0.0\bin\FSharp.Compiler.CodeDom.dll
    $provider= New-ObjectMicrosoft.FSharp.Compiler.CodeDom.FSharpCodeProvider
    <#
    如果要使用-CompilerParameters
    与-ReferencedAssemblies 不能同时使用
    $cp= New-ObjectSystem.CodeDom.Compiler.CompilerParameters
    $cp.CompilerOptions= "--platform:x86"
    #>
    $fSharpType= Add-Type-TypeDefinition$fSharpCode -CodeDomProvider$provider -ReferencedAssemblies$ra -ErrorActionIgnore -PassThru | where{ $_.IsPublic}
    $fSharpType
}


============================

######################
FSharp.Data
######################


$fSharpCode = Get-Content"$env:USERPROFILE\Desktop\Data.fs" |Out-String
$references = @("C:\FSharp.Data\bin\FSharp.Data.dll")

Compiler-FSharp $fSharpCode $references

[Test]::http("http://www.baidu.com").Body.item

============================

Data.fs:

module Test

open FSharp.Data
//open FSharp.Net
// FSharp.Data 新版本中所有的函数全部归到 Data 命名空间下了。

let http (url:string) = Http.Request url