Skip to content
Merged
Changes from 1 commit
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
Next Next commit
Add tests for generic types for Marshal.OffsetOf
  • Loading branch information
jkoritzinsky committed Jul 12, 2021
commit d460c2a500045c96dca304b5a395eb7aa354064c
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,21 @@ public void OffsetOf_NoLayoutPoint_ThrowsArgumentException()
AssertExtensions.Throws<ArgumentException>(null, () => Marshal.OffsetOf<NoLayoutPoint>(nameof(NoLayoutPoint.x)));
}

[Fact]
public void OffsetOf_Field_BlittableGeneric()
{
Assert.Equal((IntPtr)4, Marshal.OffsetOf<Point2<int>>(nameof(Point2<int>.y)));
Assert.Equal((IntPtr)8, Marshal.OffsetOf<Point2<ulong>>(nameof(Point2<int>.y)));
Assert.Equal((IntPtr)4, Marshal.OffsetOf<Point2<float>>(nameof(Point2<int>.y)));
Assert.Equal((IntPtr)8, Marshal.OffsetOf<Point2<double>>(nameof(Point2<int>.y)));

// [COMPAT] Non-blittable generic types are supported in OffsetOf since they've always been allowed
// and it likely doesn't meet the bar to break back-compat.
Assert.Equal((IntPtr)1, Marshal.OffsetOf<Point2<char>>(nameof(Point2<int>.y)));
Assert.Equal((IntPtr)1, Marshal.OffsetOf<Point2<byte>>(nameof(Point2<int>.y)));
Assert.Equal((IntPtr)IntPtr.Size, Marshal.OffsetOf<Point2<string>>(nameof(Point2<int>.y)));
}

public class NonRuntimeType : Type
{
public override FieldInfo GetField(string name, BindingFlags bindingAttr)
Expand Down Expand Up @@ -496,5 +511,11 @@ struct FieldAlignmentTest_Variant
public short s; // 2 bytes
// 6 bytes of padding
};

struct Point2<T>
{
public T x;
public T y;
}
#pragma warning restore 169, 649, 618
}