From f8408d29b34630abb9e299ed4d07bc1a708dd0fb Mon Sep 17 00:00:00 2001 From: praydog Date: Tue, 26 Mar 2024 01:56:46 -0700 Subject: [PATCH] .NET: Allow proxies to be casted to IObject --- csharp-api/REFrameworkNET/Proxy.hpp | 36 ++++++++++++++++++++++++++++- csharp-api/test/Test/Test.cs | 2 +- 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/csharp-api/REFrameworkNET/Proxy.hpp b/csharp-api/REFrameworkNET/Proxy.hpp index 786cda083..99b34b10c 100644 --- a/csharp-api/REFrameworkNET/Proxy.hpp +++ b/csharp-api/REFrameworkNET/Proxy.hpp @@ -4,12 +4,16 @@ #include "./API.hpp" #include "ManagedObject.hpp" +#include "TypeDefinition.hpp" using namespace System; // Classes for proxying interfaces from auto generated assemblies to our dynamic types namespace REFrameworkNET { -public interface class IProxy { +// Inherit IObject's interface as well +// so we can easily pretend that this is an IObject, even though it just forwards the calls to the actual object +public interface class IProxy : IObject { + IObject^ GetInstance(); void SetInstance(IObject^ instance); }; @@ -18,12 +22,42 @@ where T2 : ref class public ref class Proxy : public Reflection::DispatchProxy, public IProxy, public System::IEquatable^> { public: + virtual REFrameworkNET::TypeDefinition^ GetTypeDefinition() { + return Instance->GetTypeDefinition(); + } + + virtual void* Ptr() { + return Instance->Ptr(); + } + + virtual uintptr_t GetAddress() { + return Instance->GetAddress(); + } + + virtual REFrameworkNET::InvokeRet^ Invoke(System::String^ methodName, array^ args) { + return Instance->Invoke(methodName, args); + } + + virtual bool HandleInvokeMember_Internal(System::String^ methodName, array^ args, System::Object^% result) { + return Instance->HandleInvokeMember_Internal(methodName, args, result); + } + + // For interface types + generic + virtual T As() { + return Instance->As(); + } + static T Create(IObject^ target) { auto proxy = Reflection::DispatchProxy::Create^>(); ((IProxy^)proxy)->SetInstance(target); return proxy; } + virtual IObject^ GetInstance() { + return Instance; + } + virtual void SetInstance(IObject^ instance) { Instance = instance; } diff --git a/csharp-api/test/Test/Test.cs b/csharp-api/test/Test/Test.cs index b98da2b67..6386649ef 100644 --- a/csharp-api/test/Test/Test.cs +++ b/csharp-api/test/Test/Test.cs @@ -37,7 +37,7 @@ public static void Entry() { REFrameworkNET.API.LogInfo("Scene name: " + name); // Testing autocomplete for the concrete ManagedObject - REFrameworkNET.API.LogInfo("Scene: " + scene.ToString() + ": " + (scene as REFrameworkNET.ManagedObject)?.GetTypeDefinition()?.GetFullName()?.ToString()); + REFrameworkNET.API.LogInfo("Scene: " + scene.ToString() + ": " + (scene as REFrameworkNET.IObject).GetTypeDefinition()?.GetFullName()?.ToString()); // Testing dynamic invocation float currentTimescale = scene.get_TimeScale();