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
.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
.assembly extern CORELIB
{
.publickeytoken = (B0 3F 5F 7F 11 D5 0A 3A )
.ver 4:0:0:0
}
.NET Core:
#define CORELIB "System.Runtime"
.assembly extern CORELIB
{
.publickeytoken = (B0 3F 5F 7F 11 D5 0A 3A )
.ver 4:0:0:0
}
CORELIB mscorlib (.. [CORELIB]System.Object).