public static T CastTo <T>(MochaInterface.IMeta self) where T : MochaInterface.IMeta { if (self == null) { return(null); } // Check if the object is actually the type we're trying to cast to. if (self.IsKindOf <T>()) { Type type = typeof(T); // Check if we've already cached the factory function. Func <IntPtr, bool, object> factory; if (!constructorCache.TryGetValue(type, out factory)) { factory = CreateConstructorDelegate <T>(); constructorCache.Add(type, factory); } // Call the factory function and set the casted source to the // current object. T castedObject = (T)factory(MochaInterface.IMeta.get_static_CPtr(self).Handle, false); (castedObject as MochaInterface.IMeta).m_castedSource = self; return(castedObject); } return(null); }
public static object castTo(object fromObj, Type toType) { object retval = null; MochaInterface.IMeta fromObj2 = fromObj as MochaInterface.IMeta; HandleRef hr = fromObj2.get_CPtr(); BindingFlags flags = BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public; CultureInfo culture = null; IntPtr cPtr = hr.Handle; object[] parameters = { cPtr, false }; object toObj = Activator.CreateInstance(toType, flags, null, parameters, culture); // make sure it actually is what we think it is if (fromObj.GetType().IsInstanceOfType(toObj)) { return(toObj); } return(retval); }
void QueueAction(IntPtr cs_ptr, string _methodname, IntPtr data, string data_type) { object data_obj = null; if (data != null) { MochaInterface.IMeta imeta_obj = new MochaInterface.IMeta(data, false); if (String.IsNullOrWhiteSpace(data_type)) { Logger.Log("ERROR: Failed to convert queued event parameter because data_type is null"); } else { data_obj = CastingHelper.castTo(imeta_obj, Type.GetType("MochaInterface." + data_type)); } } MochaInterface.CScript cs = new MochaInterface.CScript(cs_ptr, false); BoundScript bs = cscriptToBoundScriptLookup[cs]; bs.scriptEvents.Enqueue(new ScriptEvent() { methodname = _methodname, data = data_obj }); }