Esempio n. 1
0
        public void WriteData(AMFWriter writer, object data)
        {
            NameObjectCollectionBase collection = data as NameObjectCollectionBase;

            object[] attributes = collection.GetType().GetCustomAttributes(typeof(DefaultMemberAttribute), false);
            if (attributes != null && attributes.Length > 0)
            {
                DefaultMemberAttribute defaultMemberAttribute = attributes[0] as DefaultMemberAttribute;
                PropertyInfo           pi = collection.GetType().GetProperty(defaultMemberAttribute.MemberName, new Type[] { typeof(string) });
                if (pi != null)
                {
                    ASObject aso = new ASObject();
                    for (int i = 0; i < collection.Keys.Count; i++)
                    {
                        string key   = collection.Keys[i];
                        object value = pi.GetValue(collection, new object[] { key });
                        aso.Add(key, value);
                    }
                    writer.WriteByte(AMF3TypeCode.Object);
                    writer.WriteAMF3Object(aso);
                    return;
                }
            }

            //We could not access an indexer so write out as it is.
            writer.WriteByte(AMF3TypeCode.Object);
            writer.WriteAMF3Object(data);
        }
Esempio n. 2
0
        public void WriteData(AMFWriter writer, object data)
        {
            NameObjectCollectionBase base2 = data as NameObjectCollectionBase;

            object[] customAttributes = base2.GetType().GetCustomAttributes(typeof(DefaultMemberAttribute), false);
            if ((customAttributes != null) && (customAttributes.Length > 0))
            {
                DefaultMemberAttribute attribute = customAttributes[0] as DefaultMemberAttribute;
                PropertyInfo           property  = base2.GetType().GetProperty(attribute.MemberName, new Type[] { typeof(string) });
                if (property != null)
                {
                    ASObject obj2 = new ASObject();
                    for (int i = 0; i < base2.Keys.Count; i++)
                    {
                        string key  = base2.Keys[i];
                        object obj3 = property.GetValue(base2, new object[] { key });
                        obj2.Add(key, obj3);
                    }
                    writer.WriteByte(10);
                    writer.WriteAMF3Object(obj2);
                    return;
                }
            }
            writer.WriteByte(10);
            writer.WriteAMF3Object(data);
        }
Esempio n. 3
0
        public void WriteData(AMFWriter writer, object data)
        {
            NameObjectCollectionBase collection = data as NameObjectCollectionBase;

            object[] attributes = collection.GetType().GetCustomAttributes(typeof(DefaultMemberAttribute), false);
            if (attributes != null && attributes.Length > 0)
            {
                DefaultMemberAttribute defaultMemberAttribute = attributes[0] as DefaultMemberAttribute;
                PropertyInfo           pi = collection.GetType().GetProperty(defaultMemberAttribute.MemberName, new Type[] { typeof(string) });
                if (pi != null)
                {
                    ASObject aso = new ASObject();

                    IEnumerator enumList = collection.Keys.GetEnumerator();

                    //为了兼容 webplayer 和手机 platform 而改掉的代码,原来的代码见后面的注释内容.
                    while (enumList.MoveNext())
                    {
                        string key   = enumList.Current as string;
                        object value = pi.GetValue(collection, new object[] { key });

                        aso.Add(key, value);
                    }
                    writer.WriteByte(AMF3TypeCode.Object);
                    writer.WriteAMF3Object(aso);
                    return;
                }
            }

            //We could not access an indexer so write out as it is.
            writer.WriteByte(AMF3TypeCode.Object);
            writer.WriteAMF3Object(data);
        }
Esempio n. 4
0
        private static void XmlSerializeNameObjects(XmlWriter writer, NameObjectCollectionBase obj, Type valueType)
        {
            Type objType = obj.GetType();

            writer.WriteStartElement(GetCleanName(objType.Name));
            bool writeValueType = (valueType == null) || valueType.Equals(Generic <object> .Type);

            if (!writeValueType)
            {
                writer.WriteAttributeString("valueType", GetTypeName(valueType));
            }
            object[] values = objType.GetMethod("BaseGetAllValues", BindingFlags.NonPublic | BindingFlags.Instance, null, Type.EmptyTypes, null).Invoke(obj, null) as object[];
            for (int i = 0; i < values.Length; i++)
            {
                writer.WriteStartElement("Item");
                writer.WriteAttributeString("name", obj.Keys[i]);
                if (values[i] != null)
                {
                    Type type = values[i].GetType();
                    if (!((!writeValueType && (type == valueType)) || IsKnownType(type)))
                    {
                        writer.WriteAttributeString("valueType", GetTypeName(type));
                    }
                }
                XmlWriterSerialize(writer, values[i], valueType);
                writer.WriteEndElement();
            }
            writer.WriteEndElement();
        }
Esempio n. 5
0
        /// <summary>
        /// 获取某个属性的值
        /// </summary>
        /// <param name="container">数据源</param>
        /// <param name="propName">属性名</param>
        /// <param name="exist">是否存在此属性</param>
        /// <returns>属性值</returns>
        internal static object GetPropertyValue(object container, string propName, out bool exist)
        {
            exist = false;
            object value = null;

            if (container == null)
            {
                throw new ArgumentNullException("container");
            }
            if (string.IsNullOrEmpty(propName))
            {
                throw new ArgumentNullException("propName");
            }
            if (Utility.IsInteger(propName))
            {
                #region 索引值部分
                //属性名只为数字.则取数组索引
                int index = Utility.ConverToInt32(propName);
                if (container is IList)
                {
                    IList iList = (IList)container;
                    if (iList.Count > index)
                    {
                        exist = true;
                        value = iList[index];
                    }
                }
                else if (container is ICollection)
                {
                    ICollection ic = (ICollection)container;
                    if (ic.Count > index)
                    {
                        exist = true;
                        IEnumerator ie = ic.GetEnumerator();
                        int         i  = 0;
                        while (i++ <= index)
                        {
                            ie.MoveNext();
                        }
                        value = ie.Current;
                    }
                }
                else
                {
                    //判断是否含有索引属性
                    PropertyInfo item = container.GetType().GetProperty("Item", new Type[] { typeof(int) });
                    if (item != null)
                    {
                        try
                        {
                            value = item.GetValue(container, new object[] { index });
                            exist = true;
                        }
                        catch
                        {
                            exist = false;
                        }
                    }
                }
                #endregion
            }
            else
            {
                #region 字段/属性/键值
                //容器是类型.则查找静态属性或字段
                Type         type  = container is Type ? (Type)container : container.GetType();
                BindingFlags flags = BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.IgnoreCase;
                if (!(container is Type))
                {
                    flags |= BindingFlags.Instance;
                }

                //查找字段
                FieldInfo field = type.GetField(propName, flags);
                if (field != null)
                {
                    exist = true;
                    value = field.GetValue(container);
                }
                else
                {
                    //查找属性
                    PropertyInfo property = type.GetProperty(propName, flags, null, null, Type.EmptyTypes, new ParameterModifier[0]);
                    if (property != null)
                    {
                        exist = true;
                        value = property.GetValue(container, null);
                    }
                    else if (container is ICustomTypeDescriptor)
                    {
                        //已实现ICustomTypeDescriptor接口
                        ICustomTypeDescriptor ictd       = (ICustomTypeDescriptor)container;
                        PropertyDescriptor    descriptor = ictd.GetProperties().Find(propName, true);
                        if (descriptor != null)
                        {
                            exist = true;
                            value = descriptor.GetValue(container);
                        }
                    }
                    else if (container is IDictionary)
                    {
                        //是IDictionary集合
                        IDictionary idic = (IDictionary)container;
                        if (idic.Contains(propName))
                        {
                            exist = true;
                            value = idic[propName];
                        }
                    }
                    else if (container is NameObjectCollectionBase)
                    {
                        //是NameObjectCollectionBase派生对象
                        NameObjectCollectionBase nob = (NameObjectCollectionBase)container;

                        //调用私有方法
                        MethodInfo method = nob.GetType().GetMethod("BaseGet", BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance, null, new Type[] { typeof(string) }, new ParameterModifier[] { new ParameterModifier(1) });
                        if (method != null)
                        {
                            value = method.Invoke(container, new object[] { propName });
                            exist = value != null;
                        }
                    }
                    else
                    {
                        //判断是否含有索引属性
                        PropertyInfo item = type.GetProperty("Item", new Type[] { typeof(string) });
                        if (item != null)
                        {
                            try
                            {
                                value = item.GetValue(container, new object[] { propName });
                                exist = true;
                            }
                            catch
                            {
                                exist = false;
                            }
                        }
                    }
                }
                #endregion
            }
            return(value);
        }
Esempio n. 6
0
        /// <summary>
        /// 获取某个属性的值
        /// </summary>
        /// <param name="container">数据源</param>
        /// <param name="propName">属性名</param>
        /// <param name="exist">是否存在此属性</param>
        /// <returns>属性值</returns>
        internal static object GetPropertyValue(object container, string propName, out bool exist)
        {
            exist = false;
            object value = null;

            if (container == null)
            {
                throw new ArgumentNullException("container");
            }
            if (string.IsNullOrEmpty(propName))
            {
                throw new ArgumentNullException("propName");
            }
            PropertyDescriptor descriptor = TypeDescriptor.GetProperties(container).Find(propName, true);

            if (descriptor != null)
            {
                exist = true;
                value = descriptor.GetValue(container);
            }
            else if (container is IDictionary)
            {
                //是IDictionary集合
                IDictionary idic = (IDictionary)container;
                if (idic.Contains(propName))
                {
                    exist = true;
                    value = idic[propName];
                }
            }
            else if (container is NameObjectCollectionBase)
            {
                NameObjectCollectionBase nob = (NameObjectCollectionBase)container;
                MethodInfo method            = nob.GetType().GetMethod("BaseGet", BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance, null, new Type[] { typeof(string) }, new ParameterModifier[] { new ParameterModifier(1) });
                if (method != null)
                {
                    value = method.Invoke(container, new object[] { propName });
                    exist = value == null;
                }
            }
            else if (container is Type)
            {
                Type type = (Type)container;
                //查找字段
                FieldInfo field = type.GetField(propName, BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.IgnoreCase);
                if (field != null)
                {
                    exist = true;
                    value = field.GetValue(container);
                }
                else
                {
                    //查找属性
                    PropertyInfo property = type.GetProperty(propName, BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.IgnoreCase, null, null, new Type[0], new ParameterModifier[0]);
                    if (property != null)
                    {
                        exist = true;
                        value = property.GetValue(container, null);
                    }
                    else
                    {
                        //查找方法
                        MethodInfo method = type.GetMethod(propName, BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.IgnoreCase, null, new Type[0], new ParameterModifier[0]);
                        if (method != null)
                        {
                            value = method.Invoke(container, null);
                            exist = value == null;
                        }
                    }
                }
            }
            return(value);
        }