Skip to content

Commit

Permalink
native: получить значение перечисления
Browse files Browse the repository at this point in the history
  • Loading branch information
Mr-Rm committed Dec 30, 2024
1 parent 0130828 commit 5c36082
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/OneScript.Core/TypeUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public static bool IsNumeric(this Type type)
switch (Type.GetTypeCode(type))
{
case TypeCode.Int32:
return !type.IsEnum;
case TypeCode.Decimal:
case TypeCode.UInt32:
case TypeCode.Int64:
Expand Down
18 changes: 18 additions & 0 deletions src/OneScript.Native/Compiler/ExpressionHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,24 @@ private static Expression TryFindConversionOp(Expression value, Type targetType)

return DowncastDecimal(decimalNum, targetType);
}
else if (targetType.IsEnum)
{
Type generic = typeof(ClrEnumValueWrapper<>);
var wrapperType = generic.MakeGenericType(new[]{targetType});
try
{
var wrapper = Expression.Convert(value, wrapperType);
return Expression.Property(wrapper,"UnderlyingValue");
}
catch (InvalidOperationException)
{
throw new NativeCompilerException(
BilingualString.Localize(
$"Преобразование {value.Type} в тип {targetType} недоступно",
$"Conversion from {value.Type} to {targetType} is unavailable")
);
}
}
else
{
var conversion = TryConvertBslValueToPrimitiveType(value, targetType);
Expand Down

0 comments on commit 5c36082

Please sign in to comment.