简体   繁体   中英

Cannot find AssemblyBuilder methods in F# project

I am trying to use an AssemblyBuilder, in order to generate an assembly in F#, according to https://docs.microsoft.com/en-us/dotnet/api/system.reflection.emit.assemblybuilder?view=netcore-3.0 . The problem is that many of the methods and properties described there are not available.

Edit: the following code works when run from F# interactive, but Visual Studio still complains that it cannot find different methods.

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")
;;

For example, AppDomain.CurrentDomain.DefineDynamicAssembly , AssemblyBuilderAccess.RunAndSave or AssemblyBuilder.Save are not available, am I using a different version? This is my .fsproj file:

<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>

The problem is not with F#, but with .NET Core. Those methods have not been ported, see Is there any replace of AssemblyBuilder.DefineDynamicAssembly in .NET Core? .

For instance, RunAndSave is supported in .NET 4.8 (the last .NET Framework version).

But it is 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 , which is supported but, curiously, doesn't yet save the assembly..

Note that you can still use DefineDynamicAssembly , but it cannot be saved, see here . If you can rewrite your code to use Framework, that'll solve your issue.

By this doc , there is no support for AppDomain.CurrentDomain.DefineDynamicAssembly

Replaced by AssemblyBuilder.DefineDynamicAssembly

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM