示例#1
0
        public void onEndObject(object o)
        {
            Type objectType = o.GetType();
            Dictionary <string, object> propertyValues = object2PropertyValues[o];

            if (propertyValues == null)
            {
                throw new SerializationException("unknown object terminated: " + o.GetType().Name + " " + o.ToString());
            }
            // for each field if its mandatory check it exists
            foreach (FieldInfo fi in objectType.GetFields(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Default))
            {
                object [] memberinfo = fi.GetCustomAttributes(typeof(DataMember), true);
                if (memberinfo.Count() > 1)
                {
                    throw new SerializationException("field " + objectType.Name + "." + fi.Name + " found " + memberinfo.Count() + " DataMember s!");
                }
                else if (memberinfo.Count() == 1)
                {
                    DataMember dm = (DataMember)memberinfo[0];

                    if (dm.IsRequired)
                    {
                        if (!propertyValues.ContainsKey(fi.Name))
                        {
                            throw new SerializationException("mandatory (DataMember.IsRequired) field " + objectType.Name + "." + fi.Name + " not specified ");
                        }
                    }
                }
            }
        }
示例#2
0
        public int expand(object o, OnChildNode listener)
        {
            if (o == null)
            {
                return(0);
            }
            Type         theclass = o.GetType();
            BindingFlags bf       = BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Default;

            FieldInfo[]       fis         = theclass.GetFields(bf);
            List <DataMember> dataMembers = new List <DataMember>();
            Dictionary <string, FieldInfo> dataMemberName2FieldInfo = new Dictionary <string, FieldInfo>();

            foreach (FieldInfo fi in fis)
            {
                object[] customAttributes = fi.GetCustomAttributes(typeof(DataMember), true);
                if (customAttributes.Length == 0)
                {
                    continue;
                }
                DataMember dm = (DataMember)customAttributes[0];

                if (dm.Name == null)
                {
                    dm.Name = fi.Name;
                }
                dataMembers.Add(dm);
                dataMemberName2FieldInfo[dm.Name] = fi;
            }

            Dictionary <string, PropertyInfo> dataMemberName2PropertyInfo = new Dictionary <string, PropertyInfo>();

            PropertyInfo[] pis = theclass.GetProperties(bf);
            foreach (PropertyInfo pi in pis)
            {
                object[] customAttributes = pi.GetCustomAttributes(typeof(DataMember), true);
                if (customAttributes.Length == 0)
                {
                    continue;
                }
                DataMember dm = (DataMember)customAttributes[0];

                if (dm.Name == null)
                {
                    dm.Name = pi.Name;
                }
                dataMembers.Add(dm);
                dataMemberName2PropertyInfo[dm.Name] = pi;
            }

            Comparison <DataMember> sorter = (dm, dm2) =>
            {
                if (dm.Order == dm2.Order)
                {
                    return(0);
                }
                else
                {
                    return(dm.Order > dm2.Order ? 1 : -1);
                }
            };

            dataMembers.Sort(sorter);
            for (int done = 0; done < dataMembers.Count; done++)
            {
                DataMember dm = dataMembers[done];
                Object     value;
                if (dataMemberName2FieldInfo.ContainsKey(dm.Name))
                {
                    FieldInfo fi = dataMemberName2FieldInfo[dm.Name];
                    value = fi.GetValue(o);
                }
                else
                {
                    PropertyInfo pi = dataMemberName2PropertyInfo[dm.Name];
                    value = pi.GetValue(o);
                }
                listener(o, dm.Name, value);
            }

            return(dataMembers.Count);
        }
示例#3
0
        public override ClassDefinition GetClassDefinition(object instance)
        {
            ClassDefinition classDefinition = null;
            Type            type            = instance.GetType();
            //Verify [DataContract] or [Serializable] on type
            bool serializable = IsDataContract(type) || type.IsSerializable;

            if (!serializable && type.Assembly != typeof(AMFWriter).Assembly)
            {
                throw new FluorineException(string.Format("Type {0} was not marked as a data contract.", type.FullName));
            }
            List <string>      memberNames     = new List <string>();
            List <ClassMember> classMemberList = new List <ClassMember>();

            PropertyInfo[] propertyInfos = type.GetProperties(BindingFlags.Public | BindingFlags.Instance);
            for (int i = 0; i < propertyInfos.Length; i++)
            {
                PropertyInfo propertyInfo = propertyInfos[i] as PropertyInfo;
                string       name         = propertyInfo.Name;
                if (propertyInfo.GetCustomAttributes(typeof(TransientAttribute), true).Length > 0)
                {
                    continue;
                }
                if (propertyInfo.GetGetMethod() == null || propertyInfo.GetGetMethod().GetParameters().Length > 0)
                {
                    //The gateway will not be able to access this property
                    string msg = __Res.GetString(__Res.Reflection_PropertyIndexFail, string.Format("{0}.{1}", type.FullName, propertyInfo.Name));
                    if (log.IsWarnEnabled)
                    {
                        log.Warn(msg);
                    }
                    continue;
                }
                object[] customAttributes = propertyInfo.GetCustomAttributes(typeof(System.Runtime.Serialization.DataMemberAttribute), false);
                if ((customAttributes != null) && (customAttributes.Length > 0))
                {
                    System.Runtime.Serialization.DataMemberAttribute attribute = (System.Runtime.Serialization.DataMemberAttribute)customAttributes[0];
                    if (attribute.Name != null && attribute.Name.Length != 0)
                    {
                        name = attribute.Name;
                    }
                }
                else
                {
                    if (!type.IsSerializable && type.Assembly != typeof(AMFWriter).Assembly)
                    {
                        continue;
                    }
                }
                if (memberNames.Contains(name))
                {
                    continue;
                }
                memberNames.Add(name);
                BindingFlags bf = BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance;
                try
                {
                    PropertyInfo propertyInfoTmp = type.GetProperty(name);
                }
                catch (AmbiguousMatchException)
                {
                    bf = BindingFlags.DeclaredOnly | BindingFlags.Static | BindingFlags.Public | BindingFlags.Instance;
                }
                object[]    attributes  = propertyInfo.GetCustomAttributes(false);
                ClassMember classMember = new ClassMember(name, bf, propertyInfo.MemberType, attributes);
                classMemberList.Add(classMember);
            }
            FieldInfo[] fieldInfos = type.GetFields(BindingFlags.Public | BindingFlags.Instance);
            for (int i = 0; i < fieldInfos.Length; i++)
            {
                FieldInfo fieldInfo = fieldInfos[i] as FieldInfo;
                if (fieldInfo.GetCustomAttributes(typeof(NonSerializedAttribute), true).Length > 0)
                {
                    continue;
                }
                if (fieldInfo.GetCustomAttributes(typeof(TransientAttribute), true).Length > 0)
                {
                    continue;
                }
                string   name             = fieldInfo.Name;
                object[] customAttributes = fieldInfo.GetCustomAttributes(typeof(System.Runtime.Serialization.DataMemberAttribute), false);
                if ((customAttributes != null) && (customAttributes.Length > 0))
                {
                    System.Runtime.Serialization.DataMemberAttribute attribute = (System.Runtime.Serialization.DataMemberAttribute)customAttributes[0];
                    if (attribute.Name != null && attribute.Name.Length != 0)
                    {
                        name = attribute.Name;
                    }
                }
                else
                {
                    if (!type.IsSerializable && type.Assembly != typeof(AMFWriter).Assembly)
                    {
                        continue;
                    }
                }
                object[]    attributes  = fieldInfo.GetCustomAttributes(false);
                ClassMember classMember = new ClassMember(name, BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance, fieldInfo.MemberType, attributes);
                classMemberList.Add(classMember);
            }
            ClassMember[] classMembers    = classMemberList.ToArray();
            string        customClassName = type.FullName;

            customClassName = FluorineConfiguration.Instance.GetCustomClass(customClassName);
            classDefinition = new ClassDefinition(customClassName, classMembers, GetIsExternalizable(instance), GetIsDynamic(instance));
            return(classDefinition);
        }