示例#1
0
            protected override object MapObjectToNative(IActionScriptSerializer serializer, Type nativeType,
                                                        ASClass @class, IEnumerable <IASValue> memberValues,
                                                        IEnumerable <KeyValuePair <string, IASValue> > dynamicProperties,
                                                        IExternalizable externalizableValue)
            {
                IDictionary <TKey, TValue> dict = CreateDictionaryInstance(@class.MemberNames.Count);

                // Add members.
                int memberIndex = 0;

                foreach (IASValue memberValue in memberValues)
                {
                    TKey   key   = (TKey)Convert.ChangeType(@class.MemberNames[memberIndex], typeof(TKey));
                    TValue value = (TValue)serializer.ToNative(memberValue, typeof(TValue));
                    dict.Add(key, value);

                    memberIndex += 1;
                }

                // Add dynamic properties.
                foreach (KeyValuePair <string, IASValue> pair in dynamicProperties)
                {
                    TKey   key   = (TKey)Convert.ChangeType(pair.Key, typeof(TKey));
                    TValue value = (TValue)serializer.ToNative(pair.Value, typeof(TValue));
                    dict.Add(key, value);
                }

                return(dict);
            }
示例#2
0
            /// <summary>
            /// Sets a mapped property or field value using reflection.
            /// </summary>
            /// <param name="serializer"></param>
            /// <param name="instance"></param>
            /// <param name="propertyOrField"></param>
            /// <param name="value"></param>
            private static void SetMappedPropertyOrField(IActionScriptSerializer serializer, object instance, MemberInfo propertyOrField, IASValue value)
            {
                try
                {
                    // Try to set the property.
                    PropertyInfo property = propertyOrField as PropertyInfo;
                    if (property != null)
                    {
                        object propertyValue = serializer.ToNative(value, property.PropertyType);
                        property.SetValue(instance, propertyValue, null);
                        return;
                    }

                    // Oh, it must be a field then.
                    FieldInfo field      = (FieldInfo)propertyOrField;
                    object    fieldValue = serializer.ToNative(value, field.FieldType);
                    field.SetValue(instance, fieldValue);
                }
                catch (Exception ex)
                {
                    throw new ActionScriptException(String.Format(CultureInfo.CurrentCulture,
                                                                  "Error while setting value of property or field named '{0}' on class '{1}'",
                                                                  propertyOrField.Name, propertyOrField.ReflectedType.FullName), ex);
                }
            }
示例#3
0
            protected override object MapArrayToNative(IActionScriptSerializer serializer, Type nativeType,
                                                       int indexedLength,
                                                       IEnumerable <IASValue> indexedValues,
                                                       IEnumerable <KeyValuePair <string, IASValue> > dynamicProperties)
            {
                IDictionary <TKey, TValue> dict = CreateDictionaryInstance(indexedLength);

                // Add indexed values.
                int index = 0;

                foreach (IASValue indexedValue in indexedValues)
                {
                    TKey   key   = (TKey)Convert.ChangeType(index, typeof(TKey));
                    TValue value = (TValue)serializer.ToNative(indexedValue, typeof(TValue));
                    dict.Add(key, value);

                    index += 1;
                }

                // Add dynamic properties.
                foreach (KeyValuePair <string, IASValue> pair in dynamicProperties)
                {
                    TKey   key   = (TKey)Convert.ChangeType(pair.Key, typeof(TKey));
                    TValue value = (TValue)serializer.ToNative(pair.Value, typeof(TValue));
                    dict.Add(key, value);
                }

                return(dict);
            }
示例#4
0
            protected override object MapByteArrayToNative(IActionScriptSerializer serializer, Type nativeType,
                                                           int length, IEnumerable <ArraySegment <byte> > segments)
            {
                ICollection <T> collection = CreateCollectionInstance(length);

                if (typeof(T) == typeof(byte))
                {
                    ICollection <byte> byteCollection = (ICollection <byte>)collection;

                    foreach (ArraySegment <byte> segment in segments)
                    {
                        for (int i = 0; i < segment.Count; i++)
                        {
                            byteCollection.Add(segment.Array[i + segment.Offset]);
                        }
                    }
                }
                else
                {
                    foreach (ArraySegment <byte> segment in segments)
                    {
                        for (int i = 0; i < segment.Count; i++)
                        {
                            IASValue byteValue = new ASInt29(segment.Array[i + segment.Offset]);
                            T        value     = (T)serializer.ToNative(byteValue, typeof(T));
                            collection.Add(value);
                        }
                    }
                }

                return(collection);
            }
示例#5
0
        private object FromAMF(byte[] bytes)
        {
            IActionScriptSerializer serializer = serializerFactory.CreateSerializer();
            MemoryStream            stream     = new MemoryStream(bytes);
            AMFDataInput            dataInput  = new AMFDataInput(stream, serializer);

            dataInput.BeginObjectStream();
            IASValue asValue = dataInput.ReadObject();

            dataInput.EndObjectStream();
            object nativeValue = serializer.ToNative(asValue, null);

            return(nativeValue);
        }
示例#6
0
            protected override object MapArrayToNative(IActionScriptSerializer serializer, Type nativeType,
                                                       int indexedLength,
                                                       IEnumerable <IASValue> indexedValues,
                                                       IEnumerable <KeyValuePair <string, IASValue> > dynamicProperties)
            {
                if (dynamicProperties.GetEnumerator().MoveNext())
                {
                    throw new ActionScriptException("Cannot map an ActionScript array with dynamic properties to a native Array type.");
                }

                ICollection <T> collection = CreateCollectionInstance(indexedLength);

                // Add indexed values.
                foreach (IASValue indexedValue in indexedValues)
                {
                    T value = (T)serializer.ToNative(indexedValue, typeof(T));
                    collection.Add(value);
                }

                return(collection);
            }
示例#7
0
            protected override object MapArrayToNative(IActionScriptSerializer serializer, Type nativeType,
                                                       int indexedLength, IEnumerable <IASValue> indexedValues,
                                                       IEnumerable <KeyValuePair <string, IASValue> > dynamicProperties)
            {
                if (dynamicProperties.GetEnumerator().MoveNext())
                {
                    throw new ActionScriptException("Cannot map an ActionScript array with dynamic properties to a native Array type.");
                }

                T[] array = new T[indexedLength];

                // Add indexed values.
                int index = 0;

                foreach (IASValue indexedValue in indexedValues)
                {
                    T value = (T)serializer.ToNative(indexedValue, typeof(T));
                    array[index++] = value;
                }

                return(array);
            }
示例#8
0
            protected override object MapByteArrayToNative(IActionScriptSerializer serializer, Type nativeType,
                                                           int length, IEnumerable <ArraySegment <byte> > segments)
            {
                // Map to a byte array if that's what we asked for or if we asked for
                // a supertype of byte[], like object[] or Array.
                if (nativeType.IsAssignableFrom(typeof(byte[])))
                {
                    byte[] bytes = new byte[length];

                    int offset = 0;
                    foreach (ArraySegment <byte> segment in segments)
                    {
                        Array.Copy(segment.Array, segment.Offset, bytes, offset, segment.Count);
                        offset += segment.Count;
                    }

                    return(bytes);
                }
                else
                {
                    // Otherwise convert the elements one at a time.
                    T[] array = new T[length];

                    int elementIndex = 0;
                    foreach (ArraySegment <byte> segment in segments)
                    {
                        for (int i = 0; i < segment.Count; i++)
                        {
                            T elementValue = (T)serializer.ToNative(new ASInt29(segment.Array[i]), typeof(T));
                            array[elementIndex++] = elementValue;
                        }
                    }

                    return(array);
                }
            }
示例#9
0
 protected T ToNative <T>(IASValue asValue)
 {
     return((T)serializer.ToNative(asValue, typeof(T)));
 }
            /// <summary>
            /// Sets a mapped property or field value using reflection.
            /// </summary>
            /// <param name="serializer"></param>
            /// <param name="instance"></param>
            /// <param name="propertyOrField"></param>
            /// <param name="value"></param>
            private static void SetMappedPropertyOrField(IActionScriptSerializer serializer, object instance, MemberInfo propertyOrField, IASValue value)
            {
                try
                {
                    // Try to set the property.
                    PropertyInfo property = propertyOrField as PropertyInfo;
                    if (property != null)
                    {
                        object propertyValue = serializer.ToNative(value, property.PropertyType);
                        property.SetValue(instance, propertyValue, null);
                        return;
                    }

                    // Oh, it must be a field then.
                    FieldInfo field = (FieldInfo)propertyOrField;
                    object fieldValue = serializer.ToNative(value, field.FieldType);
                    field.SetValue(instance, fieldValue);
                }
                catch (Exception ex)
                {
                    throw new ActionScriptException(String.Format(CultureInfo.CurrentCulture,
                        "Error while setting value of property or field named '{0}' on class '{1}'",
                        propertyOrField.Name, propertyOrField.ReflectedType.FullName), ex);
                }
            }