Open floatyears opened 2 years ago
例如这个class:
public class TestDelegateAsCtorArg { public delegate void Callback(); public TestDelegateAsCtorArg(Callback callback) { SetCallBack(callback); } public void SetCallBack(Callback callback) { if(callback != null) { callback(); } } }
生成的wrapper class的代码:
...... public global::TestDelegateAsCtorArg.Callback _TestDelegateAsCtorArg__ctor_callback; public GCHandle _TestDelegateAsCtorArg__ctor_callback_ref; static Delegate5605cacf TestDelegateAsCtorArg__ctor_callbackAction = OnTestDelegateAsCtorArg__ctor_callback; static void OnTestDelegateAsCtorArg__ctor_callback(IntPtr arg0_h) { Exception __e = null; try { var arg0Obj = ObjectStore.Get<global::TestDelegateAsCtorArg>(arg0_h); arg0Obj._TestDelegateAsCtorArg__ctor_callback(); } catch(Exception e) { __e = e; } if(__e != null) ScriptEngine.OnException(__e.ToString()); } public global::TestDelegateAsCtorArg.Callback _TestDelegateAsCtorArg_SetCallBack_callback; public GCHandle _TestDelegateAsCtorArg_SetCallBack_callback_ref; static Delegate5605cacf TestDelegateAsCtorArg_SetCallBack_callbackAction = OnTestDelegateAsCtorArg_SetCallBack_callback; static void OnTestDelegateAsCtorArg_SetCallBack_callback(IntPtr arg0_h) { Exception __e = null; try { var arg0Obj = ObjectStore.Get<global::TestDelegateAsCtorArg>(arg0_h); arg0Obj._TestDelegateAsCtorArg_SetCallBack_callback(); } catch(Exception e) { __e = e; } if(__e != null) ScriptEngine.OnException(__e.ToString()); } ...... public TestDelegateAsCtorArg(Callback callback) { if(typeof(TestDelegateAsCtorArg) == GetWType()) { var TestDelegateAsCtorArg__ctor_callback_p = Marshal.GetFunctionPointerForDelegate(TestDelegateAsCtorArg__ctor_callbackAction); var h = MonoBind.TestDelegateAsCtorArg__ctor_6(TestDelegateAsCtorArg__ctor_callback_p); ScriptEngine.CheckException(); SetHandle(h); ObjectStore.Store(this, h); } } public void SetCallBack(Callback callback) { _TestDelegateAsCtorArg_SetCallBack_callback = callback; ObjectStore.RefMember(this,ref _TestDelegateAsCtorArg_SetCallBack_callback_ref,_TestDelegateAsCtorArg_SetCallBack_callback); var TestDelegateAsCtorArg_SetCallBack_callback_p = Marshal.GetFunctionPointerForDelegate(TestDelegateAsCtorArg_SetCallBack_callbackAction); MonoBind.TestDelegateAsCtorArg_SetCallBack(this.Handle, TestDelegateAsCtorArg_SetCallBack_callback_p); ScriptEngine.CheckException(); return ; }
生成的UnityBind的代码:
static Delegatef55bb3df _EventDelegate__ctor_call; static void OnEventDelegate__ctor_call(this global::EventDelegate arg0) { var arg0_h = ObjectStore.Store(arg0); _EventDelegate__ctor_call(arg0_h); ScriptEngine.CheckException(); } [MonoPInvokeCallback(typeof(EventDelegate__ctor_12_Type))] static IntPtr EventDelegate__ctor_12 (IntPtr call_p) { Exception __e = null; try { _EventDelegate__ctor_call = call_p == IntPtr.Zero ? null: Marshal.GetDelegateForFunctionPointer<Delegatef55bb3df>(call_p); var _value = new global::EventDelegate(thizObj.OnEventDelegate__ctor_call); //语法错误 var _valueHandle = ObjectStore.Store(_value); return _valueHandle; } catch(Exception _e_) { __e = _e_; } if(__e != null) ScriptEngine.OnException(__e.ToString()); return default(IntPtr); }
项目目前对于delegate的处理应该是将mono层传入的delegate作为function pointer,赋值给wrapper class的static成员。mono内的wrapper class传入的delegate没有正确赋值,UnityBInd内被封装之后的delegate的实例则依赖于class的实例来获得,而此时还在构造constructor的参数,无法获取到实例,导致最后生成的代码产生语法错误。
例如这个class:
生成的wrapper class的代码:
生成的UnityBind的代码:
项目目前对于delegate的处理应该是将mono层传入的delegate作为function pointer,赋值给wrapper class的static成员。mono内的wrapper class传入的delegate没有正确赋值,UnityBInd内被封装之后的delegate的实例则依赖于class的实例来获得,而此时还在构造constructor的参数,无法获取到实例,导致最后生成的代码产生语法错误。