Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions samples/ArrayLayout.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#:project ../src/ObjectLayoutInspector/ObjectLayoutInspector.csproj
#:property PublishAot=false

using ObjectLayoutInspector;

Console.WriteLine("--- Primitive array: int[5] ---");
ArrayLayout.PrintLayout<int>(5);

Console.WriteLine("--- Struct array with inline fields: Point[3] ---");
ArrayLayout.PrintLayout<Point>(3);

Console.WriteLine("--- Struct array with values: Point[3] ---");
var points = new Point[] { new(1, 2), new(3, 4), new(5, 6) };
ArrayLayout.PrintLayout(points);

Console.WriteLine("--- Nullable array with values: int?[4] ---");
var nullables = new int?[] { 42, null, 7, null };
ArrayLayout.PrintLayout((Array)nullables);

Console.WriteLine("--- Reference type array: string[4] ---");
ArrayLayout.PrintLayout<string>(4);

record struct Point(int X, int Y);
14 changes: 14 additions & 0 deletions samples/ClassLayout.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#:project ../src/ObjectLayoutInspector/ObjectLayoutInspector.csproj
#:property PublishAot=false

using ObjectLayoutInspector;

Console.WriteLine("=== Class Layouts ===");
Console.WriteLine();

Console.WriteLine("--- String ---");
TypeLayout.PrintLayout<string>();
Console.WriteLine();

Console.WriteLine("--- Exception ---");
TypeLayout.PrintLayout<Exception>();
36 changes: 36 additions & 0 deletions samples/CustomStruct.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#:project ../src/ObjectLayoutInspector/ObjectLayoutInspector.csproj
#:property PublishAot=false

using System.Runtime.InteropServices;
using ObjectLayoutInspector;

Console.WriteLine("=== Custom Struct Layouts ===");
Console.WriteLine();

Console.WriteLine("--- MyPoint (no padding) ---");
TypeLayout.PrintLayout<MyPoint>();
Console.WriteLine();
ArrayLayout.PrintLayout<MyPoint>(3);

Console.WriteLine("--- MyEvent (with padding) ---");
TypeLayout.PrintLayout<MyEvent>();
Console.WriteLine();
ArrayLayout.PrintLayout<MyEvent>(2);

[StructLayout(LayoutKind.Sequential)]
struct MyPoint
{
public float X;
public float Y;
public float Z;
}

#pragma warning disable CS0649 // Fields are intentionally never assigned — used only for layout inspection
struct MyEvent
{
public long Timestamp;
public int EventId;
public byte Priority;
public bool IsProcessed;
}
#pragma warning restore CS0649
18 changes: 18 additions & 0 deletions samples/NullableLayout.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#:project ../src/ObjectLayoutInspector/ObjectLayoutInspector.csproj
#:property PublishAot=false

using ObjectLayoutInspector;

Console.WriteLine("=== Nullable Type Layouts ===");
Console.WriteLine();

Console.WriteLine("--- int? ---");
TypeLayout.PrintLayout<int?>();
Console.WriteLine();

Console.WriteLine("--- DateTime? ---");
TypeLayout.PrintLayout<DateTime?>();
Console.WriteLine();

Console.WriteLine("--- bool? ---");
TypeLayout.PrintLayout<bool?>();
14 changes: 14 additions & 0 deletions samples/StructLayout.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#:project ../src/ObjectLayoutInspector/ObjectLayoutInspector.csproj
#:property PublishAot=false

using ObjectLayoutInspector;

Console.WriteLine("=== Struct Layouts ===");
Console.WriteLine();

Console.WriteLine("--- DateTime ---");
TypeLayout.PrintLayout<DateTime>();
Console.WriteLine();

Console.WriteLine("--- Guid ---");
TypeLayout.PrintLayout<Guid>();
31 changes: 31 additions & 0 deletions src/ObjectLayoutInspector.Tests/NullableTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,15 @@ public void Nullable()
Assert.AreEqual(Unsafe.SizeOf<Nullable<bool>>(), typeLayout.Size);
}

// Regression test for https://github.com/SergeyTeplyakov/ObjectLayoutInspector/issues/40
[Test]
public void NullableInt_HasFields()
{
var typeLayout = TypeLayout.GetLayout<int?>(includePaddings: false);
Assert.AreEqual(Unsafe.SizeOf<int?>(), typeLayout.Size);
Assert.That(typeLayout.Fields.Length, Is.GreaterThan(0), "Nullable<int> layout should have fields");
}

[Test]
public void NullableLongByteStruct()
{
Expand Down Expand Up @@ -100,5 +109,27 @@ public void WithNullableIntStructRecursive()
Assert.AreEqual(4, unsafeLayout[1].Offset);
Assert.AreEqual(4, unsafeLayout[1].Size);
}

[Test]
public void NullableArrayWithValues()
{
// Exact same code path as the sample: ArrayLayout.PrintLayout((Array)nullables)
var nullables = new int?[] { 42, null, 7, null };
var layout = ArrayLayout.GetLayout(typeof(int?), nullables.Length);
var result = layout.ToStringWithValues(nullables);
Assert.That(result, Does.Contain("42"));
Assert.That(result, Does.Contain("null"));
}

[Test]
public void StructArrayWithValues()
{
var points = new WithNullableIntStruct[]
{
default,
default
};
ArrayLayout.PrintLayout(points);
}
}
}
Loading
Loading