Last active
July 2, 2024 09:43
-
-
Save lemonmojo/41e03c502f0c0831fc319af71b71536b to your computer and use it in GitHub Desktop.
Beyond.NET interface adapter
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 is declared by the user. | |
public interface IInterface1 | |
{ | |
void MethodInIInterface1(); | |
} | |
// This should be auto-generated to allow Swift to provide implementations for .NET interfaces. | |
public class IInterface1_Adapter : IInterface1 | |
{ | |
public delegate void MethodInIInterface1_Delegate(); | |
private MethodInIInterface1_Delegate _MethodInIInterface1_Adapter; | |
public IInterface1_Adapter(MethodInIInterface1_Delegate methodInIInterface1_Adapter) | |
{ | |
_MethodInIInterface1_Adapter = methodInIInterface1_Adapter; | |
} | |
public void MethodInIInterface1() | |
{ | |
_MethodInIInterface1_Adapter(); | |
} | |
} | |
// This is declared by the user. | |
public class TypeThatUsesInterfaces | |
{ | |
public void CallMethod1InIInterface1(IInterface1 interface1) | |
{ | |
interface1.MethodInIInterface1(); | |
} | |
} |
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
func testInterfaceAdapter() throws { | |
let typeThatUsesInterfaces = try Beyond_NET_Sample_TypeThatUsesInterfaces() | |
let methodInIInterface1CalledExpectation = expectation(description: "IInterface1.MethodInIInterface1 called in Swift") | |
// NOTE: Compiler ensures we provide all interface requirements. | |
let interface1Adapter = try Beyond_NET_Sample_IInterface1_Adapter(.init({ | |
print("IInterface1.MethodInIInterface1 called in Swift") | |
methodInIInterface1CalledExpectation.fulfill() | |
})) | |
try typeThatUsesInterfaces.callMethod1InIInterface1(try interface1Adapter.castTo()) | |
wait(for: [ methodInIInterface1CalledExpectation ]) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment