Using assemblies compiled from IL with .NET Core & Xamarin

后端 未结 2 926
粉色の甜心
粉色の甜心 2021-02-09 08:18

Updated with a solution that works for me. See the bottom of this question.

Context:
I needed a way to evaluate the size of a generic type for the p

相关标签:
2条回答
  • 2021-02-09 09:03

    Ok, this is the result of my research (based on https://github.com/dotnet/corefx/tree/master/src/System.Runtime.CompilerServices.Unsafe and https://github.com/jvbsl/ILProj):

    global.json:

    {
      "msbuild-sdks": {
        "Microsoft.NET.Sdk.IL": "3.0.0-preview-27318-01"
      }
    }
    

    ILProj\ILProj.ilproj:

    <Project Sdk="Microsoft.NET.Sdk.IL">
    
        <PropertyGroup>
            <TargetFramework>netstandard2.0</TargetFramework>
            <MicrosoftNetCoreIlasmPackageVersion>3.0.0-preview-27318-01</MicrosoftNetCoreIlasmPackageVersion>
            <IncludePath Condition="'$(TargetFramework)' == 'netstandard1.0'">include\netstandard</IncludePath>
            <IncludePath Condition="'$(TargetFramework)' == 'netstandard2.0'">include\netstandard</IncludePath>
            <IncludePath Condition="'$(TargetFramework)' == 'netcoreapp1.0'">include\netcoreapp</IncludePath>
            <IncludePath Condition="'$(TargetFramework)' == 'netcoreapp2.0'">include\netcoreapp</IncludePath>
            <IlasmFlags>$(IlasmFlags) -INCLUDE=$(IncludePath)</IlasmFlags>
        </PropertyGroup>
    
    </Project>
    

    ILProj\include\netstandard\coreassembly.h:

    #define CORE_ASSEMBLY "System.Runtime"
    
    // Metadata version: v4.0.30319
    .assembly extern CORE_ASSEMBLY
    {
      .publickeytoken = ( B0 3F 5F 7F 11 D5 0A 3A )
      .ver 4:0:0:0
    }
    
    

    ILProj\Class.il

    #include "coreassembly.h"
    
    .assembly ILProj
    {
      .ver 1:0:0:0
    }
    
    .module ILProj.dll
    
    .class public abstract auto ansi sealed beforefieldinit ILProj.Class
      extends [CORE_ASSEMBLY]System.Object
    {
      .method public hidebysig static int32 Square(int32 a) cil managed
      {
        .maxstack 2
        ldarg.0
        dup
        mul
        ret
      }
    }
    

    If you have difficulties to put it all together, then look at the example here: https://github.com/Konard/ILProj

    0 讨论(0)
  • 2021-02-09 09:21

    There is a very good example of how to do it correctly in the DotNet CoreFX repo. System.Runtime.CompilerServices.Unsafe is an IL only assembly and can be used by .NET Core and Xamarin.

    https://github.com/dotnet/corefx/tree/master/src/System.Runtime.CompilerServices.Unsafe

    There are two approaches to the problem: (i) try to recreate required elements of the build configuration in your project from scratch - what will be time consuming and very involved - corefx build system is really complex, (ii) use existing build infrastructure and create your IL project inside .NET Core CoreFX repo by replicating System.Runtime.CompilerServices.Unsafe project, changing naming and and replacing IL code with yours. In my opinion this is the fastest way to build IL based assembly which will be guaranteed to work properly with all targets.

    To build your assembly while targeting any particular version of .NET Core or .NET Standard just create it in the release branches: release/2.0.0, release/1.1.0 etc.

    The type 'Object' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Runtime, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null'.

    There is an option to try for suppressing that compilation error alone in a project referencing assembly that triggers it. Putting the following property to a new format csproj/vbproj should suppress it:

    <PropertyGroup>
        <_HasReferenceToSystemRuntime>true</_HasReferenceToSystemRuntime>
    </PropertyGroup>
    
    0 讨论(0)
提交回复
热议问题