Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -223,4 +223,21 @@ internal static TypeSyntax GetIdentifierName (string []? @namespace, string @cla
/// <returns>The identifier name expression for a given class.</returns>
internal static TypeSyntax GetIdentifierName (string @class)
=> IdentifierName (@class);

/// <summary>
/// Create the needed expression to call the AsRef method from the Unsafe class.
/// </summary>
/// <param name="objectType">The target type for get the ref from.</param>
/// <param name="arguments">The arguments to pass to the AsRef method.</param>
/// <returns>The needed expression to call the AsRef method.</returns>
internal static ExpressionSyntax AsRef (string objectType, ImmutableArray<ArgumentSyntax> arguments)
{
var unsafeType = GetIdentifierName (
@namespace: ["System", "Runtime", "CompilerServices"],
@class: "Unsafe",
isGlobal: true);
var argsList = ArgumentList (SeparatedList<ArgumentSyntax> (arguments.ToSyntaxNodeOrTokenArray ()));
return StaticInvocationGenericExpression (unsafeType, "AsRef",
objectType, argsList);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -900,4 +900,36 @@ void GetIdentifierNameTests (string []? @namespace, string @class, bool isGlobal
Assert.Equal (expectedDeclaration, declaration.ToFullString ());
}

class TestDataAsRefTests : IEnumerable<object []> {
public IEnumerator<object []> GetEnumerator ()
{
yield return [
"NSObject",
ImmutableArray.Create (
Argument (IdentifierName ("arg1"))
),
"global::System.Runtime.CompilerServices.Unsafe.AsRef<NSObject> (arg1)"
];

yield return [
"NSString",
ImmutableArray.Create (
Argument (IdentifierName ("arg1")),
Argument (IdentifierName ("arg2")),
Argument (IdentifierName ("arg3"))),
"global::System.Runtime.CompilerServices.Unsafe.AsRef<NSString> (arg1, arg2, arg3)"
];
}

IEnumerator IEnumerable.GetEnumerator () => GetEnumerator ();
}

[Theory]
[ClassData (typeof (TestDataAsRefTests))]
void AsRefTests (string objectType, ImmutableArray<ArgumentSyntax> arguments, string expectedDeclaration)
{
var declaration = AsRef (objectType, arguments);
Assert.Equal (expectedDeclaration, declaration.ToFullString ());
}

}