在 F# 项目中找不到 AssemblyBuilder 方法
Cannot find AssemblyBuilder methods in F# project
根据 https://docs.microsoft.com/en-us/dotnet/api/system.reflection.emit.assemblybuilder?view=netcore-3.0,我正在尝试使用 AssemblyBuilder,以便在 F# 中生成程序集。问题是其中描述的许多方法和属性不可用。
编辑:当 运行 来自 F# interactive 时,以下代码有效,但 Visual Studio 仍然抱怨找不到不同的方法。
open System
open System.Reflection
open System.Reflection.Emit
let generateAssembly (program: CILProgram) (fileName: string) : unit =
let assemblyName = new AssemblyName ("name")
let assemblyBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.RunAndSave)
assemblyBuilder.Save("name")
;;
例如,AppDomain.CurrentDomain.DefineDynamicAssembly
、AssemblyBuilderAccess.RunAndSave
或AssemblyBuilder.Save
不可用,我使用的是其他版本吗?这是我的 .fsproj 文件:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp3.0</TargetFramework>
<IsPackable>false</IsPackable>
<GenerateProgramFile>false</GenerateProgramFile>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="nunit" Version="3.12.0" />
<PackageReference Include="NUnit3TestAdapter" Version="3.13.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.2.0" />
</ItemGroup>
<ItemGroup>
<Compile Include="AbstractSyntax.fs" />
<Compile Include="Parser.fs" />
<Compile Include="Lexer.fs" />
<Compile Include="Helpers.fs" />
<Compile Include="TypeChecker.fs" />
<Compile Include="CILCompiler.fs" />
<Compile Include="CILGenerator.fs" />
<Compile Include="Program.fs" />
<Compile Include="ParserTest.fs" />
<Compile Include="TypeCheckerTest.fs" />
</ItemGroup>
<ItemGroup>
<Reference Include="FsLexYacc.Runtime">
<HintPath>..\..\..\source\FsLexYacc\tests\LexAndYaccMiniProject\bin\Debug\net46\FsLexYacc.Runtime.dll</HintPath>
</Reference>
</ItemGroup>
</Project>
<TargetFramework>netcoreapp3.0</TargetFramework>
问题不在于 F#,而在于 .NET Core。这些方法尚未移植,请参阅 。
例如,RunAndSave
is supported in .NET 4.8(最新的 .NET Framework 版本)。
但它是 not supported in .NET Core, or even .NET 5.0. There has been discussion on this dating back to 2015, and this proposal has info and a mention that it wasn't considered for .NET 5.0. You can also see it in the resolution of Regex.CompileToAssembly
,它受支持,但奇怪的是,还没有保存程序集..
请注意,您仍然可以使用DefineDynamicAssembly
,但无法保存,see here。如果您可以重写代码以使用 Framework,那将解决您的问题。
据此doc,不支持AppDomain.CurrentDomain.DefineDynamicAssembly
替换为AssemblyBuilder.DefineDynamicAssembly
根据 https://docs.microsoft.com/en-us/dotnet/api/system.reflection.emit.assemblybuilder?view=netcore-3.0,我正在尝试使用 AssemblyBuilder,以便在 F# 中生成程序集。问题是其中描述的许多方法和属性不可用。
编辑:当 运行 来自 F# interactive 时,以下代码有效,但 Visual Studio 仍然抱怨找不到不同的方法。
open System
open System.Reflection
open System.Reflection.Emit
let generateAssembly (program: CILProgram) (fileName: string) : unit =
let assemblyName = new AssemblyName ("name")
let assemblyBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.RunAndSave)
assemblyBuilder.Save("name")
;;
例如,AppDomain.CurrentDomain.DefineDynamicAssembly
、AssemblyBuilderAccess.RunAndSave
或AssemblyBuilder.Save
不可用,我使用的是其他版本吗?这是我的 .fsproj 文件:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp3.0</TargetFramework>
<IsPackable>false</IsPackable>
<GenerateProgramFile>false</GenerateProgramFile>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="nunit" Version="3.12.0" />
<PackageReference Include="NUnit3TestAdapter" Version="3.13.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.2.0" />
</ItemGroup>
<ItemGroup>
<Compile Include="AbstractSyntax.fs" />
<Compile Include="Parser.fs" />
<Compile Include="Lexer.fs" />
<Compile Include="Helpers.fs" />
<Compile Include="TypeChecker.fs" />
<Compile Include="CILCompiler.fs" />
<Compile Include="CILGenerator.fs" />
<Compile Include="Program.fs" />
<Compile Include="ParserTest.fs" />
<Compile Include="TypeCheckerTest.fs" />
</ItemGroup>
<ItemGroup>
<Reference Include="FsLexYacc.Runtime">
<HintPath>..\..\..\source\FsLexYacc\tests\LexAndYaccMiniProject\bin\Debug\net46\FsLexYacc.Runtime.dll</HintPath>
</Reference>
</ItemGroup>
</Project>
<TargetFramework>netcoreapp3.0</TargetFramework>
问题不在于 F#,而在于 .NET Core。这些方法尚未移植,请参阅
例如,RunAndSave
is supported in .NET 4.8(最新的 .NET Framework 版本)。
但它是 not supported in .NET Core, or even .NET 5.0. There has been discussion on this dating back to 2015, and this proposal has info and a mention that it wasn't considered for .NET 5.0. You can also see it in the resolution of Regex.CompileToAssembly
,它受支持,但奇怪的是,还没有保存程序集..
请注意,您仍然可以使用DefineDynamicAssembly
,但无法保存,see here。如果您可以重写代码以使用 Framework,那将解决您的问题。
据此doc,不支持AppDomain.CurrentDomain.DefineDynamicAssembly
替换为AssemblyBuilder.DefineDynamicAssembly