Skip to content

Commit

Permalink
Fixed Task return type for SharpAutoCompatibilityWrapper wrapped meth…
Browse files Browse the repository at this point in the history
…ods.
  • Loading branch information
Uralstech committed Aug 11, 2024
1 parent 044e3e9 commit c523a3f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public abstract class EzrSharpCompatibilityWrapper(Context parentContext, Positi
public static bool IsSupportedPrimitiveType(Type type)
{
TypeCode typeCode = Type.GetTypeCode(type);

return typeCode is TypeCode.Empty
or TypeCode.Int16
or TypeCode.Int32
Expand All @@ -48,6 +49,20 @@ or TypeCode.Char
or TypeCode.String;
}

/// <summary>
/// Checks if the given type is supported by the primitive compatibility wrappers, including generic <see cref="Task"/> objects.
/// </summary>
/// <param name="type">The type to check.</param>
/// <returns><see langword="true"/> if yes, <see langword="false"/> otherwise.</returns>
public static bool IsSupportedReturnType(Type type)
{
return Type.GetTypeCode(type) switch
{
TypeCode.Object when typeof(Task).IsAssignableFrom(type) => type.GenericTypeArguments.Length == 0 || IsSupportedPrimitiveType(type.GenericTypeArguments[0]),
_ => IsSupportedPrimitiveType(type),
};
}

/// <summary>
/// Converts an ezr² type to a C# primitive type.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public class SharpAutoCompatibilityWrapperAttribute : Attribute
if (methodInfo.IsGenericMethod)
return new($"The \"{nameof(SharpAutoCompatibilityWrapperAttribute)}\" attribute does not support generic method \"{methodInfo.Name}\".", nameof(methodInfo));

if (methodInfo.ReturnType != typeof(void) && !EzrSharpCompatibilityWrapper.IsSupportedPrimitiveType(methodInfo.ReturnType))
if (methodInfo.ReturnType != typeof(void) && !EzrSharpCompatibilityWrapper.IsSupportedReturnType(methodInfo.ReturnType))
return new($"Expected method \"{methodInfo.Name}\"'s return type to be of a supported primitive type, as it uses the attribute \"{nameof(SharpAutoCompatibilityWrapperAttribute)}\"", nameof(methodInfo));

foreach (ParameterInfo parameterInfo in methodInfo.GetParameters())
Expand Down

0 comments on commit c523a3f

Please sign in to comment.