示例#1
0
        private object ReadActionScriptObject(bool typed)
        {
            ActionScriptObject aso = new ActionScriptObject();

            if (typed)
            {
                aso.TypeName = ReadUTF();
            }

            Dictionary <string, object> values = aso.Properties;

            //get the first key
            string key = ReadUTF();
            //get the first type
            AMFDataType dataType = (AMFDataType)ReadByte();

            while (dataType != AMFDataType.EndOfObject)
            {
                values[key] = ParseAMFDataForType(dataType);

                key      = ReadUTF();
                dataType = (AMFDataType)ReadByte();
            }

            return(aso);
        }
示例#2
0
        public static object Deserialize(ActionScriptObject aso, Type type)
        {
            if (aso == null)
            {
                return(null);
            }

            //create an instance of the type
            object rehydrated = Activator.CreateInstance(type);

            //now we have to go through all of the properties and attributes
            DeserializeProperties(aso, type, rehydrated);

            return(rehydrated);
        }
        private void WriteActionScriptObject(ActionScriptObject aso)
        {
            if (aso.TypeName != string.Empty && aso.TypeName != null)
            {
                Write((byte)AMFDataType.TypedObject);
                WriteUTF(aso.TypeName);
            }
            else
            {
                Write((byte)AMFDataType.Object);
            }

            foreach (KeyValuePair <string, object> kvp in aso.Properties)
            {
                WriteUTF(kvp.Key);

                AMFData data = kvp.Value as AMFData;
                if (data == null)
                {
                    Write((byte)AMFDataType.Null);
                }
                else
                {
                    //this will allow us to have nested objects as properties
                    //on our AMF objects
                    if (data.DataType == AMFDataType.AMFEnabledObject)
                    {
                        if (data.Data != null)
                        {
                            ProcessUnknownObject(data.Data);
                        }
                        else
                        {
                            Write((byte)AMFDataType.Null);
                        }
                    }
                    else
                    {
                        WriteAMFData(data.DataType, data.Data);
                    }
                }
            }

            //write out the end record
            Write(Convert.ToUInt16(0));
            Write((byte)AMFDataType.EndOfObject);
        }
        public static ActionScriptObject TransformDataSet(DataSet ds)
        {
            ActionScriptObject aso = new ActionScriptObject();

            aso.TypeName = "Kamacho.DNF.DTO.DataSetDTO";

            List <ActionScriptObject> dataTables = new List <ActionScriptObject>();

            foreach (DataTable dt in ds.Tables)
            {
                dataTables.Add(ProcessDataTable(dt));
            }

            aso.Properties.Add("tables", new AMFData(AMFDataType.Array, dataTables));

            return(aso);
        }
        private static ActionScriptObject ProcessDataTable(DataTable dt)
        {
            ActionScriptObject aso = new ActionScriptObject();

            aso.TypeName = "Kamacho.DNF.DTO.DataTableDTO";

            List <ActionScriptObject> columns = new List <ActionScriptObject>();

            //now go through the columns

            Dictionary <string, AMFDataType> dataTypes = new Dictionary <string, AMFDataType>();

            foreach (DataColumn dc in dt.Columns)
            {
                ActionScriptObject column = new ActionScriptObject();
                column.TypeName = "Kamacho.DNF.DTO.DataColumnDTO";

                column.Properties.Add("name", new AMFData(AMFDataType.String, dc.ColumnName));

                AMFDataType columnType = AMFDataType.Unsupported;

                Type dataType = dc.DataType;
                if (dataType == typeof(Boolean))
                {
                    columnType = AMFDataType.Boolean;
                }
                else if (dataType == typeof(String))
                {
                    columnType = AMFDataType.LongString;
                }
                else if (dataType == typeof(DateTime))
                {
                    columnType = AMFDataType.Date;
                }
                else if (dataType == typeof(Byte) ||
                         dataType == typeof(Decimal) ||
                         dataType == typeof(Double) ||
                         dataType == typeof(Int16) ||
                         dataType == typeof(Int32) ||
                         dataType == typeof(SByte) ||
                         dataType == typeof(Single) ||
                         dataType == typeof(TimeSpan) ||
                         dataType == typeof(UInt16) ||
                         dataType == typeof(UInt32) ||
                         dataType == typeof(UInt64))
                {
                    columnType = AMFDataType.Number;
                }
                else
                {
                    columnType = AMFDataType.String;
                }


                column.Properties.Add("dataType", new AMFData(AMFDataType.String, columnType));
                dataTypes.Add(dc.ColumnName, columnType);

                columns.Add(column);
            }

            aso.Properties.Add("columns", new AMFData(AMFDataType.Array, columns));

            //now we can go through the rows
            List <ActionScriptObject> rows = new List <ActionScriptObject>();

            foreach (DataRow dr in dt.Rows)
            {
                ActionScriptObject row = new ActionScriptObject();
                row.TypeName = "Kamacho.DNF.DTO.DataRowDTO";

                //get the values for the row
                List <ActionScriptObject> values = new List <ActionScriptObject>();
                foreach (DataColumn dc2 in dt.Columns)
                {
                    ActionScriptObject rowValue = new ActionScriptObject();
                    rowValue.TypeName = "Kamacho.DNF.DTO.DataRowValueDTO";
                    rowValue.Properties.Add("name", new AMFData(AMFDataType.String, dc2.ColumnName));
                    rowValue.Properties.Add("data", new AMFData(dataTypes[dc2.ColumnName], dr[dc2]));

                    values.Add(rowValue);
                }

                row.Properties.Add("values", new AMFData(AMFDataType.Array, values));
                rows.Add(row);
            }

            aso.Properties.Add("rows", new AMFData(AMFDataType.Array, rows));

            return(aso);
        }
        public object GetTypedObject(int index, Type typeToReturn)
        {
            ActionScriptObject aso = GetActionScriptObject(index);

            return(AMFDeserializer.Deserialize(aso, typeToReturn));
        }
示例#7
0
        private static void DeserializeFields(ActionScriptObject aso, Type type, object instance)
        {
            //get the custom attributes of the object
            FieldInfo[] fields = type.GetFields();
            if (fields != null)
            {
                foreach (FieldInfo field in fields)
                {
                    object[] attributes = field.GetCustomAttributes(typeof(AMFProperty), true);
                    if (attributes != null)
                    {
                        foreach (AMFProperty propertyAttribute in attributes)
                        {
                            string      key          = propertyAttribute.Name;
                            AMFDataType propertyType = propertyAttribute.ActionScriptDataType;

                            //if we don't have let default value take over
                            if (!aso.Properties.ContainsKey(key))
                            {
                                continue;
                            }

                            AMFData data = aso.Properties[key] as AMFData;

                            if (data.Data == null)
                            {
                                continue;
                            }

                            switch (propertyType)
                            {
                            case AMFDataType.AMFEnabledObject:
                                if (field.FieldType.Name == "Object")
                                {
                                    field.SetValue(instance, data.Data);
                                }
                                else
                                {
                                    field.SetValue(instance, Deserialize(data.Data as ActionScriptObject, field.FieldType));
                                }
                                break;

                            case AMFDataType.Array:
                                ArrayList al       = data.Data as ArrayList;
                                ArrayList alValues = new ArrayList();
                                if (al != null && al.Count > 0)
                                {
                                    object[] arrayItemAttributes = field.GetCustomAttributes(typeof(AMFArrayItem), true);

                                    Dictionary <string, Type> itemMap = new Dictionary <string, Type>();
                                    if (arrayItemAttributes != null)
                                    {
                                        foreach (AMFArrayItem arrayItemAttribute in arrayItemAttributes)
                                        {
                                            itemMap.Add(arrayItemAttribute.TypeName, arrayItemAttribute.TargetType);
                                        }
                                    }

                                    foreach (AMFData alData in al)
                                    {
                                        object dataValue = null;

                                        if (alData.Data is ActionScriptObject)
                                        {
                                            ActionScriptObject arrayItemASO = alData.Data as ActionScriptObject;
                                            if (itemMap.ContainsKey(arrayItemASO.TypeName))
                                            {
                                                dataValue = GetArrayElementValue(alData, itemMap[arrayItemASO.TypeName]);
                                            }
                                            else
                                            {
                                                dataValue = GetArrayElementValue(alData, field.FieldType.GetElementType());
                                            }
                                        }
                                        else
                                        {
                                            dataValue = GetArrayElementValue(alData, field.FieldType.GetElementType());
                                        }


                                        if (dataValue != null)
                                        {
                                            alValues.Add(dataValue);
                                        }
                                    }

                                    field.SetValue(instance, alValues.ToArray(field.FieldType.GetElementType()));
                                }
                                break;

                            case AMFDataType.Boolean:
                            case AMFDataType.LongString:
                            case AMFDataType.String:
                            case AMFDataType.UTCDate:
                            case AMFDataType.Date:
                                field.SetValue(instance, data.Data);
                                break;

                            case AMFDataType.Guid:
                                field.SetValue(instance, new Guid(data.Data.ToString()));
                                break;

                            case AMFDataType.Number:
                                switch (field.FieldType.Name)
                                {
                                case "Int32":
                                    field.SetValue(instance, Convert.ToInt32(data.Data));
                                    break;

                                case "Int16":
                                    field.SetValue(instance, Convert.ToInt16(data.Data));
                                    break;

                                case "Int64":
                                    field.SetValue(instance, Convert.ToInt64(data.Data));
                                    break;

                                case "Decimal":
                                    field.SetValue(instance, Convert.ToDecimal(data.Data));
                                    break;
                                }
                                break;
                            }
                        }
                    }
                }
            }
        }
        private void ProcessUnknownObject(object data)
        {
            //if no data, then get out
            if (data == null)
            {
                return;
            }

            //could be anything, but we are going to send it back
            //as a typed object
            Type t = data.GetType();

            object[] attributes = t.GetCustomAttributes(true);
            bool     ok         = false;

            ActionScriptObject aso = new ActionScriptObject();

            if (attributes != null)
            {
                foreach (Attribute attribute in attributes)
                {
                    if (attribute is AMFEnabled)
                    {
                        ok           = true;
                        aso.TypeName = ((AMFEnabled)attribute).ActionScriptTypeName;
                        break;
                    }
                }

                if (ok)
                {
                    //get the properties and fields for this type and then we can get those attributes
                    //that we need to export
                    FieldInfo[] fields = t.GetFields();
                    if (fields != null)
                    {
                        foreach (FieldInfo field in fields)
                        {
                            attributes = field.GetCustomAttributes(typeof(AMFProperty), true);
                            if (attributes != null)
                            {
                                foreach (AMFProperty propertyAttribute in attributes)
                                {
                                    string      key          = propertyAttribute.Name;
                                    AMFDataType propertyType = propertyAttribute.ActionScriptDataType;

                                    //get the value
                                    object propertyValue = field.GetValue(data);

                                    //save it
                                    AMFData amfData = new AMFData();
                                    amfData.DataType    = propertyType;
                                    amfData.Data        = propertyValue;
                                    aso.Properties[key] = amfData;
                                }
                            }
                        }
                    }

                    //do the same thing for the props
                    PropertyInfo[] properties = t.GetProperties();
                    if (properties != null)
                    {
                        foreach (PropertyInfo property in properties)
                        {
                            attributes = property.GetCustomAttributes(typeof(AMFProperty), true);
                            if (attributes != null)
                            {
                                foreach (AMFProperty propertyAttribute in attributes)
                                {
                                    string      key          = propertyAttribute.Name;
                                    AMFDataType propertyType = propertyAttribute.ActionScriptDataType;

                                    //get the value

                                    object propertyValue = property.GetValue(data, null);

                                    //save it
                                    AMFData amfData = new AMFData();
                                    amfData.DataType    = propertyType;
                                    amfData.Data        = propertyValue;
                                    aso.Properties[key] = amfData;
                                }
                            }
                        }
                    }
                }
            }

            if (!ok)
            {
                if (data is ActionScriptObject)
                {
                    WriteActionScriptObject(data as ActionScriptObject);
                }
                else if (data is int)
                {
                    Write((byte)AMFDataType.Number);
                    Write(Convert.ToDouble(data));
                }
                else
                {
                    throw new Exception("Type returned for Flex was not marked as AMFEnabled: " + t.ToString());
                }
            }
            else
            {
                WriteActionScriptObject(aso);
            }
        }