-
Notifications
You must be signed in to change notification settings - Fork 247
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(dotnet): introduce
UnsafeCast<T>()
method
In order to allow converting an opaque object instance to an arbitrary (jsii-managed) interface type, the `UnsafeCase` operation may be used to instantiate a proxy while bypassing all type consistency checks. This is similar to using `as any` or `as T` in TypeScript, meaning that if the user performs a cast to an incorrect/unsupported type, undefined behavior follows. This would unblock certain specific use-case scenarios that static languages render difficult to enact, such as the one described in aws/aws-cdk#3284.
- Loading branch information
1 parent
3ca0e42
commit 7ecfbb4
Showing
3 changed files
with
150 additions
and
73 deletions.
There are no files selected for viewing
37 changes: 37 additions & 0 deletions
37
packages/@jsii/dotnet-runtime/src/Amazon.JSII.Runtime.UnitTests/Deputy/DeputyBaseTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
using System; | ||
using Amazon.JSII.Runtime.Deputy; | ||
using Xunit; | ||
using Xunit.Sdk; | ||
|
||
namespace Amazon.JSII.Runtime.UnitTests.Deputy | ||
{ | ||
public sealed class DeputyBaseTests | ||
{ | ||
const string Prefix = "Runtime.Deputy." + nameof(DeputyBase) + "."; | ||
|
||
[Fact(DisplayName = Prefix + nameof(CanCastToAnyInterface))] | ||
public void CanCastToAnyInterface() | ||
{ | ||
var subject = new AnonymousObject(new ByRefValue("object@10000", Array.Empty<string>())); | ||
var result = subject.UnsafeCast<IManagedInterface>(); | ||
Assert.IsType<ManagedInterfaceProxy>(result); | ||
} | ||
|
||
[JsiiInterface(typeof(IManagedInterface), "test.IManagedInterface")] | ||
private interface IManagedInterface | ||
{ | ||
bool BooleanProperty { get; } | ||
} | ||
|
||
[JsiiTypeProxy(typeof(IManagedInterface), "test.IManagedInterface")] | ||
private class ManagedInterfaceProxy : DeputyBase, IManagedInterface | ||
{ | ||
public ManagedInterfaceProxy(ByRefValue byRef): base(byRef) | ||
{ | ||
BooleanProperty = true; | ||
} | ||
|
||
public bool BooleanProperty { get; } | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters