Skip to content

Commit

Permalink
feat: Support structs in value conversions
Browse files Browse the repository at this point in the history
  • Loading branch information
mycroes committed Oct 21, 2024
1 parent 980def2 commit f7b0e1b
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
6 changes: 4 additions & 2 deletions Sally7/ValueConversion/FromS7Conversions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ internal static class FromS7Conversions
{
public static Delegate GetConverter<TValue>()
{
if (typeof(TValue).IsPrimitive || typeof(TValue).IsEnum)
// Hack, will convert structs as numerical types of equal size as the struct.
if (typeof(TValue).IsValueType)
{
return Unsafe.SizeOf<TValue>() switch
{
Expand All @@ -29,7 +30,8 @@ public static Delegate GetConverter<TValue>()
var elementType = typeof(TValue).GetElementType() ?? throw new Exception(
$"Type {typeof(TValue)} doesn't have an ElementType.");

if (elementType.IsPrimitive || elementType.IsEnum)
// Hack, will convert structs as numerical types of equal size as the struct.
if (elementType.IsValueType)
{
return ConversionHelper.SizeOf(elementType) switch
{
Expand Down
6 changes: 4 additions & 2 deletions Sally7/ValueConversion/ToS7Conversions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ internal static class ToS7Conversions
{
public static Delegate GetConverter<TValue>()
{
if (typeof(TValue).IsPrimitive || typeof(TValue).IsEnum)
// Hack, will convert structs as numerical types of equal size as the struct.
if (typeof(TValue).IsValueType)
{
return Unsafe.SizeOf<TValue>() switch
{
Expand All @@ -30,7 +31,8 @@ public static Delegate GetConverter<TValue>()
var elementType = typeof(TValue).GetElementType() ??
throw new Exception($"Type {typeof(TValue)} doesn't have an ElementType.");

if (elementType.IsPrimitive || elementType.IsEnum)
// Hack, will convert structs as numerical types of equal size as the struct.
if (elementType.IsValueType)
{
return ConversionHelper.SizeOf(elementType) switch
{
Expand Down

0 comments on commit f7b0e1b

Please sign in to comment.