Ejemplo n.º 1
0
        public static object UnwrapNullable(object obj)
        {
            obj = PhpVariable.Unwrap(obj);
            if (obj == null)
            {
                return(null);
            }

            // This could be done more efficiently, but it is not used very often...
            Type ty = obj.GetType();

            if (ty.IsGenericType && ty.GetGenericTypeDefinition() == typeof(Nullable <>))
            {
                bool val = (bool)ty.GetProperty("HasValue").GetGetMethod().Invoke(obj, Type.EmptyTypes);
                if (!val)
                {
                    return(null);
                }
                return(ty.GetProperty("Value").GetGetMethod().Invoke(obj, Type.EmptyTypes));
            }
            else
            {
                return(obj);
            }
        }
Ejemplo n.º 2
0
        public static T TryObjectToDelegate <T>(object obj, out ConversionStrictness strictness)
            where T : class
        {
            T      result   = null;
            object bare_obj = PhpVariable.Unwrap(obj);

            if (bare_obj == null || (result = bare_obj as T) != null)
            {
                strictness = ConversionStrictness.ImplExactMatch;
                return(result);
            }

            // try to convert the object to PhpCallback
            PhpCallback callback = Convert.ObjectToCallback(obj, true);

            if (callback != null && callback.Bind(true))
            {
                // generate a conversion stub
                result = EventClass <T> .GetStub(
                    callback.TargetInstance,
                    callback.TargetRoutine,
                    callback.IsBoundToCaller?callback.RoutineName : null);

                if (result != null)
                {
                    strictness = ConversionStrictness.ImplExactMatch;
                    return(result);
                }
            }

            strictness = ConversionStrictness.Failed;
            return(default(T));
        }
Ejemplo n.º 3
0
 public static T TryObjectToStruct <T>(object obj, out ConversionStrictness strictness)
     where T : struct
 {
     obj = PhpVariable.Unwrap(obj);
     if (obj is T)
     {
         strictness = ConversionStrictness.ImplExactMatch;
         return((T)obj);
     }
     else
     {
         strictness = ConversionStrictness.Failed;
         return(default(T));
     }
 }
Ejemplo n.º 4
0
        public static T TryObjectToClass <T>(object obj, out ConversionStrictness strictness)
            where T : class
        {
            if (obj == null)
            {
                strictness = ConversionStrictness.ImplDomainChange; return(null);
            }

            T result = null;

            if ((result = PhpVariable.Unwrap(obj) as T) != null && (!(result is IPhpVariable) || result is PhpObject || result is PhpArray))
            {
                strictness = ConversionStrictness.ImplExactMatch;
                return(result);
            }

            strictness = ConversionStrictness.Failed;
            return(default(T));
        }
Ejemplo n.º 5
0
        public static T[] TryObjectToArray <T>(object obj, out ConversionStrictness strictness)
        {
            T[] result = PhpVariable.Unwrap(obj) as T[];
            if (result != null)
            {
                strictness = ConversionStrictness.ImplExactMatch;
                return(result);
            }

            // try to convert PhpArray to the desired array
            PhpArray array = obj as PhpArray;

            if (array != null && array.StringCount == 0)
            {
                result = new T[array.MaxIntegerKey + 1];

                strictness = ConversionStrictness.ImplExactMatch;
                for (int i = 0; i < result.Length; i++)
                {
                    object item;
                    if (array.TryGetValue(i, out item))
                    {
                        // try to convert the item
                        ConversionStrictness tmp;
                        result[i] = TryObjectToType <T>(item, out tmp);
                        if (tmp > strictness)
                        {
                            strictness = tmp;
                        }
                        if (strictness == ConversionStrictness.Failed)
                        {
                            return(default(T[]));
                        }
                    }
                }

                return(result);
            }

            strictness = ConversionStrictness.Failed;
            return(default(T[]));
        }
Ejemplo n.º 6
0
        public static void GetObjectData(DObject /*!*/ instance, SerializationInfo /*!*/ info, StreamingContext strctx)
        {
            info.SetType(typeof(Deserializer));

            SerializationContext context = SerializationContext.CreateFromStreamingContext(strctx);

            bool     sleep_called;
            PhpArray sleep_result;

            // try to get the caches __sleep result
            if (context.SleepResults.TryGetValue(instance, out sleep_result))
            {
                if (Object.ReferenceEquals(sleep_result, SerializationContext.NoSleepResultSingleton))
                {
                    sleep_called = false;
                    sleep_result = null;
                }
                else
                {
                    sleep_called = true;
                }
            }
            else
            {
                sleep_result = instance.Sleep(context.ClassContext, context.ScriptContext, out sleep_called);
                context.SleepResults.Add(instance, (sleep_called ? sleep_result : SerializationContext.NoSleepResultSingleton));
            }

            if (sleep_called && sleep_result == null)
            {
                // __sleep did not return an array -> this instance will deserialize as NULL
                info.AddValue(__PHP_Incomplete_Class.ClassNameFieldName, String.Empty);
            }
            else
            {
                // if we have a sleep result, serialize fields according to it, otherwise serialize all fields

                IEnumerable <KeyValuePair <string, object> > serializable_properties;
                object real_object = null;

                if (sleep_result == null)
                {
                    serializable_properties = Serialization.EnumerateSerializableProperties(
                        instance,
                        true);                         // get PHP fields only

                    // serialize CLR real object in the "CLR way"
                    if (!(instance is PhpObject))
                    {
                        real_object = instance.RealObject;
                    }
                }
                else
                {
                    serializable_properties = Serialization.EnumerateSerializableProperties(
                        instance,
                        sleep_result,
                        context.ScriptContext);
                }

                bool type_name_serialized   = false;
                bool real_object_serialized = false;

                foreach (KeyValuePair <string, object> pair in serializable_properties)
                {
                    if (pair.Key == __PHP_Incomplete_Class.ClassNameFieldName)
                    {
                        type_name_serialized = true;
                    }

                    if (pair.Key == ClrRealObjectSerializationInfoKey)
                    {
                        // unwrap the possibly wrapped CLR real object
                        info.AddValue(pair.Key, PhpVariable.Unwrap(pair.Value));

                        real_object_serialized = true;
                    }
                    else
                    {
                        PhpReference reference = pair.Value as PhpReference;
                        info.AddValue(pair.Key, WrapPropertyValue(pair.Value));
                    }
                }

                // if the type name has not been serialized, do it now
                if (!type_name_serialized)
                {
                    info.AddValue(__PHP_Incomplete_Class.ClassNameFieldName, instance.TypeName);
                }

                // if the real object has not been serialized, do it now
                if (!real_object_serialized)
                {
                    info.AddValue(ClrRealObjectSerializationInfoKey, real_object);
                }
            }
        }