Using assemblies compiled from IL with .NET Core & Xamarin

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

Context:
I need a way to estimate the size of a generic type in order to calculate the length of the array so that it matches a specific byte size. Basically,   sizeofsimilar to what C / C ++ provides.

C # sizeofand Marshal.SizeOf are not suitable for this due to their many limitations.

With this in mind, I wrote an assembly in IL that allows you to use the functionality I was looking for using the opcode sizeof. I know that it essentially evaluates IntPtr.Sizewith reference types.

I duplicated this for .NET Standard and Core, referring to what, in my opinion, was the correct equivalent of mscorlib. Note that compiling IL is excellent, this question is about a different issue.

Code:
Headers for the target structure:

.NET: (Windows \ Microsoft.NET \ Framework \ v4.0.30319 \ ilasm.exe)

.assembly extern mscorlib {}

.NET Standard: (ilasm extracted from nuget )

.assembly extern netstandard 
{ 
  .publickeytoken = (B7 7A 5C 56 19 34 E0 89)
  .ver 0:0:0:0
}
.assembly extern System.Runtime
{
  .ver 0:0:0:0
}

.NET Core: (same ilasm as standard, although I tested both)

.assembly extern System.Runtime
{
  .ver 0:0:0:0
}

Source:

.assembly Company.IL
{
  .ver 0:0:1:0
}
.module Company.IL.dll

// CORE is a define for mscorlib, netstandard, and System.Runtime
.class public abstract sealed auto ansi beforefieldinit 
  Company.IL.Embedded extends [CORE]System.Object
{
  .method public hidebysig specialname rtspecialname instance void 
    .ctor() cil managed 
  {
    .maxstack 8
    ldarg.0
    call instance void [CORE]System.Object::.ctor()
    ret
  }

  .method public hidebysig static uint32 
    SizeOf<T>() cil managed 
  {
    sizeof !!0
    ret
  }
}

Problem:
When any DLL compiled this way references a .NET Core or Xamarin application, I get the following error:

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

, DLL .NET .NET, .NET-.

, , . , , , mscorlib ( ). , IL .NET Standard Core.

,.NET Standard Core , , , .

:

  • System.Runtime
  • .NET Core, #, . , , , (, System.Runtime 4.2 .NET Core 2.0).
  • Emit API. , , , , Xamarin.iOS( AOT). , .

Update:
Jacek answer ( ), corefx script VS 2017. , System.Runtime.CompilerServices.Unsafe .

, , , System.Runtime.

( corefx):

.NET:

#define CORELIB "mscorlib"

.assembly extern CORELIB {}

.NET:

#define CORELIB "System.Runtime"
#define netcoreapp

// Metadata version: v4.0.30319
.assembly extern CORELIB
{
  .publickeytoken = (B0 3F 5F 7F 11 D5 0A 3A )
  .ver 4:0:0:0
}

.NET Core:

#define CORELIB "System.Runtime"

// Metadata version: v4.0.30319
.assembly extern CORELIB
{
  .publickeytoken = (B0 3F 5F 7F 11 D5 0A 3A )
  .ver 4:0:0:0
}

CORELIB mscorlib (.. [CORELIB]System.Object).

+7
2

DotNet CoreFX , . System.Runtime.CompilerServices.Unsafe IL .NET Core Xamarin.

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

: (i) - - corefx , (ii) IL Core Core Core Core Core Core Core Core Core 2 Core 2, System.Runtime.CompilerServices.Unsafe, IL- . -, IL, .

- .NET Core .NET Standard, : release/2.0.0, release/1.1.0 ..

"" , . "System.Runtime, Version = 0.0.0.0, Culture = neutral, PublicKeyToken = null".

, , . , csproj/vbproj :

<PropertyGroup>
    <_HasReferenceToSystemRuntime>true</_HasReferenceToSystemRuntime>
</PropertyGroup>
+3

, ( https://github.com/dotnet/corefx/tree/master/src/System.Runtime.CompilerServices.Unsafe 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
  }
}

, : https://github.com/Konard/ILProj

0

All Articles