Example #1
0
        public ConfigMessageSerializerClass CreateClassInfoFromType(Type type, SerializationDefaults serializationDefaults = null)
        {
            if (serializationDefaults == null)
            {
                serializationDefaults = new SerializationDefaults();
            }
            MessageSerializedClassInfo messageSerializedClassInfo = new MessageSerializedClassInfo(type, null, serializationDefaults);

            ConfigMessageSerializerClass configMessageSerializerClass = new ConfigMessageSerializerClass();
            ConfigClassInfo configClassInfo = new ConfigClassInfo();

            configClassInfo.AssemblyName           = messageSerializedClassInfo.ClassType.Assembly.FullName;
            configClassInfo.ClassFullName          = messageSerializedClassInfo.ClassType.FullName;
            configClassInfo.MessageClassAttribute  = messageSerializedClassInfo.MessageClassAttribute;
            configMessageSerializerClass.ClassInfo = configClassInfo;

            foreach (MessageSerializedPropertyInfo messageSerializedPropertyInfo in messageSerializedClassInfo.Properties)
            {
                ConfigPropertyInfo configPropertyInfo = new ConfigPropertyInfo();
                configPropertyInfo.Name = messageSerializedPropertyInfo.PropertyInfo.Name;
                //configPropertyInfo._messagePropertyAttribute = messageSerializedPropertyInfo.MessagePropertyAttribute;
                configPropertyInfo.Attributes.Add(messageSerializedPropertyInfo.MessagePropertyAttribute);
                configClassInfo.Properties.Add(configPropertyInfo);
            }

            return(configMessageSerializerClass);
        }
Example #2
0
        protected void GetClassInfo(List <ConfigMessageSerializerClass> configMessageSerializerClasses)
        {
            MessageClassAttribute = GetMessageSerializedClassAttributeFromList(ClassType, configMessageSerializerClasses) ?? GetMessageSerializedClassAttribute();
            IsVariableLength      = false;

            List <Type> types = GetTypesToIterate();

            int totalLengthWithoutVariableData      = 0;
            int nonVaryingLengthPartOfMessageLength = 0;
            List <MessageSerializedPropertyInfo> messageSerializedProperties = new List <MessageSerializedPropertyInfo>();

            foreach (Type currentType in types)
            {
                ConfigClassInfo configClassInfo = GetConfigClassInfoFromList(currentType, configMessageSerializerClasses);
                List <MessageSerializedPropertyInfo> messageSerializedPropertiesForType = GetPropertiesForType(currentType, configClassInfo, ref totalLengthWithoutVariableData, ref nonVaryingLengthPartOfMessageLength);
                messageSerializedProperties.AddRange(messageSerializedPropertiesForType);
            }

            Properties       = messageSerializedProperties;
            CalculatedFields = GetCalculatedFields();
            TotalLengthWithoutVariableData = totalLengthWithoutVariableData;
            AssociateBlobProperties();
        }
Example #3
0
        protected List <MessageSerializedPropertyInfo> GetPropertiesForType(Type type, ConfigClassInfo configClassInfo, ref int totalLengthWithoutVariableData, ref int nonVaryingLengthPartOfMessageLength)
        {
            PropertyInfo[] objectProperties = type.GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly);
            List <MessageSerializedPropertyInfo> messageSerializedProperties = new List <MessageSerializedPropertyInfo>();
            int indexInMessage = 0;

            foreach (PropertyInfo propertyInfo in objectProperties)
            {
                MessagePropertyAttribute messagePropertyAttribute =
                    GetMessageSerializedPropertyAttributeFromClassInfo(propertyInfo, configClassInfo, MessageClassAttribute.DefaultExcludeProperty)
                    ?? GetMessageSerializedPropertyAttribute(propertyInfo, MessageClassAttribute.DefaultExcludeProperty);

                if (messagePropertyAttribute.Exclude)
                {
                    continue;
                }

                // Note: If you want to check something with the attribute you should use it after creating the MessageSerializablePropertyInfo
                // as that is what initializes any of the unset values
                MessageSerializedPropertyInfo messageSerializedPropertyInfo = new MessageSerializedPropertyInfo(indexInMessage++, propertyInfo, messagePropertyAttribute, SerializationDefaults, MessageClassAttribute);

                if (messagePropertyAttribute.MaxLength != -1 && messagePropertyAttribute.MaxLength < messagePropertyAttribute.MinLength)
                {
                    throw new Exception(string.Format("For {0} the MinLength ({1}) > MaxLength ({2}) which is not allowed",
                                                      messageSerializedPropertyInfo.PropertyInfo.Name, messageSerializedPropertyInfo.MessagePropertyAttribute.MinLength, messageSerializedPropertyInfo.MessagePropertyAttribute.MaxLength));
                }

                if (messageSerializedPropertyInfo.IsVariableLength)
                {
                    IsVariableLength = true;
                }

                int propertyLength = messageSerializedPropertyInfo.MessagePropertyAttribute.Length;
                if (messageSerializedPropertyInfo.MessagePropertyAttribute.BlobType == BlobTypes.Data)
                {
                    ContainsBlobData = true;
                }
                else if (!messageSerializedPropertyInfo.IsVariableLength)
                {
                    totalLengthWithoutVariableData += propertyLength;
                }

                messageSerializedProperties.Add(messageSerializedPropertyInfo);
            }

            return(messageSerializedProperties);
        }
Example #4
0
        protected MessagePropertyAttribute GetMessageSerializedPropertyAttributeFromClassInfo(PropertyInfo propertyInfo, ConfigClassInfo configClassInfo, bool defaultExcludeProperty)
        {
            if (configClassInfo == null)
            {
                return(null);
            }

            foreach (ConfigPropertyInfo configPropertyInfo in configClassInfo.Properties)
            {
                if (configPropertyInfo.Name == propertyInfo.Name)
                {
                    return((MessagePropertyAttribute)configPropertyInfo.Attributes.FirstOrDefault(item => item is MessagePropertyAttribute) ?? new MessagePropertyAttribute());
                }
            }

            return(GetDefaultMessageSerializedPropertyAttribute(defaultExcludeProperty));
        }
Example #5
0
        protected MessageClassAttribute GetMessageSerializedClassAttributeFromList(Type classType, List <ConfigMessageSerializerClass> configMessageSerializerClasses)
        {
            ConfigClassInfo configClassInfo = GetConfigClassInfoFromList(classType, configMessageSerializerClasses);

            return(configClassInfo?.MessageClassAttribute);
        }