/// <include file='Doc/Common.xml' path='/docs/method[@name="OnDeserialization"]/*'/> public virtual void OnDeserialization(object sender) { if (instance == null) { return; } object real_object = serInfo.GetValue(ClrRealObjectSerializationInfoKey, typeof(object)); if (real_object != null) { // if we have serialized CLR real object, populate the instance now if (instance is __PHP_Incomplete_Class) { Serialization.SetProperty( instance, ClrRealObjectSerializationInfoKey, ClrObject.WrapRealObject(real_object), context.ScriptContext); } else if (instance is IClrValue) { Type type = instance.GetType(); // generic type ClrValue<T> FieldInfo field = type.GetField("realValue"); Debug.Assert(field != null); field.SetValue(instance, real_object); } else { ((ClrObject)instance).SetRealObject(real_object); } } // deserialize fields SerializationInfoEnumerator enumerator = serInfo.GetEnumerator(); while (enumerator.MoveNext()) { string name = enumerator.Name; if (name != __PHP_Incomplete_Class.ClassNameFieldName && name != ClrRealObjectSerializationInfoKey) { Serialization.SetProperty( instance, name, UnwrapPropertyValue(enumerator.Value), context.ScriptContext); } } Serialization.DebugInstanceDeserialized(instance, true); instance.Wakeup(context.ClassContext, context.ScriptContext); }
public WrappedClrDynamicMetaObject(DynamicMetaObject dynamic) : base(dynamic.Expression, BindingRestrictions.GetTypeRestriction(dynamic.Expression, dynamic.LimitType), ClrObject.WrapRealObject(dynamic.Value) ) { Debug.Assert(!Types.DObject[0].IsAssignableFrom(dynamic.LimitType) && dynamic.Value != null && Configuration.Application.Compiler.ClrSemantics); }
public static DObject CreateClrThread(PhpCallback /*!*/ callback, params object[] args) { if (callback == null) { PhpException.ArgumentNull("callback"); } if (!callback.Bind()) { return(null); } object[] copies = (args != null) ? new object[args.Length] : ArrayUtils.EmptyObjects; for (int i = 0; i < copies.Length; i++) { copies[i] = PhpVariable.DeepCopy(args[i]); } return(ClrObject.WrapRealObject(ThreadPool.QueueUserWorkItem(new Worker(ScriptContext.CurrentContext, copies).Run, callback))); }
// Ensure that types are in a form that PHP can handle // eg. floats to doubles and collections to PHP arrays // TODO: Handle complex objects other than collections private object PhpSafeType(object o) { // PHP can handle bool, int, double, and long if ((o is int) || (o is double) || (o is long) || (o is bool)) { return(o); } // Upcast other integer types so PHP can use them // TODO: What to do about System.UInt64 and byte? else if (o is short) { return((int)(short)o); } else if (o is ushort) { return((int)(ushort)o); } else if (o is uint) { return((long)(uint)o); } else if (o is ulong) { ulong u = (ulong)o; if (u <= Int64.MaxValue) { return(System.Convert.ToInt64(u)); } else { return(u.ToString()); } } // Convert System.Single to a string // to reduce rounding errors // TODO: Figure out why I need to do this else if (o is float) { return(Double.Parse(o.ToString())); } // Really not sure what the best thing is to do with 'System.Decimal' // TODO: Review this decision else if (o is decimal) { return(o.ToString()); } // Strings and byte arrays require special handling else if (o is string) { return(new PhpString((string)o)); } else if (o is byte[]) { return(new PhpBytes((byte[])o)); } // Convert .NET collections into PHP arrays else if (o is ICollection) { var ca = new PhpArray(); if (o is IDictionary) { var dict = o as IDictionary; foreach (var key in dict.Keys) { var val = PhpSafeType(dict[key]); ca.SetArrayItem(PhpSafeType(key), val); } } else { foreach (var item in (ICollection)o) { ca.Add(PhpSafeType(item)); } } return(ca); } // PHP types are obviously ok and can just move along if (o is DObject) { return(o); } // Wrap all remaining CLR types so that PHP can handle tham return(ClrObject.WrapRealObject(o)); }