Beispiel #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);
        }
Beispiel #2
0
        public MessageSerializedClassInfo(Type classType, List <ConfigMessageSerializerClass> configMessageSerializerClasses, SerializationDefaults serializationDefaults)
        {
            ClassType             = classType;
            SerializationDefaults = serializationDefaults;
            GetClassInfo(configMessageSerializerClasses);
            SerializerClassGeneration serializerClassGeneration = new SerializerClassGeneration(serializationDefaults.TypeSelectors);

            Serializer = serializerClassGeneration.CreateSerializerClassForType(this);
        }
Beispiel #3
0
        public MessageSerializedClassInfo GetClassInfo(Type type, List <ConfigMessageSerializerClass> configMessageSerializerClasses, bool replaceIfExists, SerializationDefaults serializationDefaults = null)
        {
            lock (_lock)
            {
                if (_classInfos.ContainsKey(type.FullName))
                {
                    if (!replaceIfExists)
                    {
                        return(_classInfos[type.FullName]);
                    }

                    _classInfos.Remove(type.FullName);
                }

                if (serializationDefaults == null)
                {
                    serializationDefaults = new SerializationDefaults();
                }
                MessageSerializedClassInfo messageSerializedClassInfo = new MessageSerializedClassInfo(type, configMessageSerializerClasses, serializationDefaults);
                _classInfos.Add(type.FullName, messageSerializedClassInfo);

                return(messageSerializedClassInfo);
            }
        }
Beispiel #4
0
        public void Check(MessageSerializedPropertyInfo messageSerializedPropertyInfo, SerializationDefaults serializationDefaults, MessageClassAttribute classAttribute)
        {
            var elementType = messageSerializedPropertyInfo.ElementType;
            var messagePropertyAttribute = messageSerializedPropertyInfo.MessagePropertyAttribute;

            if (!messagePropertyAttribute.IsLengthSpecified && NumericFunctions.IsIntegerType(elementType))
            {
                // If we are using BCD the max length isn't the number of bytes of the type,
                // it's however long the maximum number is.  Sort of.
                // As an example, the MaxUInt is 4294967295, which is 10 digits.
                // So really, it is 9 digits.  Of course that doesn't quite work
                // because that is 4 1/2 bytes so really we should support 5 bytes but limit
                // how many we fill in but for now we will just say it is the minimum number
                // of bytes that things can safely be fit in.
                // elementType.GetField("MaxValue").GetValue(null).ToString()
                // The call to GetField uses Reflection to the get static MaxValue field.
                // The call to GetValue(null) gets the value of the MaxValue field (the null is because it's static)
                // Then the ToString is to figure out the number of digits that can be used.
                Type lengthType = elementType.IsEnum ? Enum.GetUnderlyingType(elementType) : elementType;
                messagePropertyAttribute.Length = messagePropertyAttribute.IsBcd ? (lengthType.GetField("MaxValue").GetValue(null).ToString().Length + 1) / 2 : Marshal.SizeOf(lengthType);
            }

            if (!messagePropertyAttribute.IsLengthSpecified && messageSerializedPropertyInfo.ElementIsMessageSerializableObject)
            {
                MessageSerializedClassInfo classInfo = Serializer.Instance.GetClassInfo(elementType);
                messagePropertyAttribute.Length = classInfo.TotalLengthWithoutVariableData;
            }

            if (!messagePropertyAttribute.IsVariableLengthSpecified && messagePropertyAttribute.BlobType == BlobTypes.Data)
            {
                messagePropertyAttribute.VariableLength = true;
            }

            if (!messagePropertyAttribute.IsVariableLengthSpecified && messagePropertyAttribute.Length == 0 && elementType.FullName == typeof(string).FullName)
            {
                messagePropertyAttribute.VariableLength = true;
            }

            if (!messagePropertyAttribute.IsMinLengthSpecified)
            {
                messagePropertyAttribute.MinLength = 0;
            }

            if (!messagePropertyAttribute.IsMaxLengthSpecified)
            {
                messagePropertyAttribute.MaxLength = -1;
            }

            if (!messagePropertyAttribute.IsMinimizeVariableLengthSpecified)
            {
                messagePropertyAttribute.MinimizeVariableLength = false;
            }
        }
Beispiel #5
0
 public MessageSerializedClassInfo GetClassInfo(Type type, bool replaceIfExists, SerializationDefaults serializationDefaults = null)
 {
     return(GetClassInfo(type, null, replaceIfExists, serializationDefaults));
 }
Beispiel #6
0
 public MessageSerializedClassInfo GetClassInfo(Type type, SerializationDefaults serializationDefaults = null)
 {
     return(GetClassInfo(type, false, serializationDefaults));
 }
Beispiel #7
0
 public void LoadSerializableClassesFromSettings(List <ConfigMessageSerializerClass> configMessageSerializerClasses, bool replaceIfExists, SerializationDefaults serializationDefaults = null)
 {
     foreach (ConfigMessageSerializerClass configMessageSerializerClass in configMessageSerializerClasses)
     {
         Type type = Type.GetType(configMessageSerializerClass.ClassInfo.AssemblyQualifiedName);
         if (type == null)
         {
             throw new Exception(string.Format("Couldn't find type {0} to create serializer class", configMessageSerializerClass.ClassInfo.AssemblyQualifiedName));
         }
         GetClassInfo(type, configMessageSerializerClasses, replaceIfExists);
     }
 }
Beispiel #8
0
 public void LoadSerializableClassesFromSettings(List <ConfigMessageSerializerClass> configMessageSerializerClasses, SerializationDefaults serializationDefaults = null)
 {
     LoadSerializableClassesFromSettings(configMessageSerializerClasses, false, serializationDefaults);
 }
Beispiel #9
0
 public void LoadSerializableClassesFromAssembly(Assembly assembly, bool replaceIfExists, SerializationDefaults serializationDefaults = null)
 {
     foreach (Type type in assembly.GetTypes())
     {
         foreach (Type interfaceType in type.GetInterfaces())
         {
             if (interfaceType == typeof(IMessageSerializable))
             {
                 GetClassInfo(type, replaceIfExists, serializationDefaults);
                 break;
             }
         }
     }
 }
Beispiel #10
0
        public void Check(MessageSerializedPropertyInfo messageSerializedPropertyInfo, SerializationDefaults serializationDefaults, MessageClassAttribute classAttribute)
        {
            if (messageSerializedPropertyInfo.ElementType.FullName == typeof(DateTime).FullName)
            {
                var messagePropertyAttribute = messageSerializedPropertyInfo.MessagePropertyAttribute;
                if (!messagePropertyAttribute.IsIsBcdSpecified)
                {
                    messagePropertyAttribute.IsBcd = true;
                }

                if (!messagePropertyAttribute.IsFormatSpecified)
                {
                    messagePropertyAttribute.Format = "MMddyyyyHHmmss";
                }

                if (!messagePropertyAttribute.IsLengthSpecified)
                {
                    messagePropertyAttribute.Length = messagePropertyAttribute.Format.Length / 2; // BCD
                }
            }
        }
Beispiel #11
0
 public void Check(MessageSerializedPropertyInfo messageSerializedPropertyInfo, SerializationDefaults serializationDefaults, MessageClassAttribute classAttribute)
 {
     if (!messageSerializedPropertyInfo.MessagePropertyAttribute.IsIsBcdSpecified && messageSerializedPropertyInfo.PropertyInfo.Name.StartsWith("Bcd", StringComparison.InvariantCultureIgnoreCase))
     {
         messageSerializedPropertyInfo.MessagePropertyAttribute.IsBcd = true;
     }
 }
Beispiel #12
0
        public void Check(MessageSerializedPropertyInfo messageSerializedPropertyInfo, SerializationDefaults serializationDefaults, MessageClassAttribute classAttribute)
        {
            if (!messageSerializedPropertyInfo.ContainsAuthenticationAttribute &&
                messageSerializedPropertyInfo.PropertyInfo.Name.StartsWith("Crc", StringComparison.InvariantCultureIgnoreCase))
            {
                messageSerializedPropertyInfo.CalculatedFieldAttributes.Add(new CalculatedAuthenticationResultAttribute(typeof(CalculatorAuthenticationCrc16)));
            }

            // By default authentication fields are excluded from length calculations
            if (!messageSerializedPropertyInfo.ContainsLengthAttribute && messageSerializedPropertyInfo.PropertyInfo.Name.StartsWith("Crc", StringComparison.InvariantCultureIgnoreCase))
            {
                messageSerializedPropertyInfo.CalculatedFieldAttributes.Add(new CalculatedLengthAttribute()
                {
                    Exclude = true
                });
            }
        }
Beispiel #13
0
        protected void CheckRules(MessagePropertyAttribute messagePropertyAttribute, PropertyInfo propertyInfo, Type elementType, bool isMessageSerializable, SerializationDefaults serializationDefaults, MessageClassAttribute classAttribute)
        {
            if (serializationDefaults is null)
            {
                return;
            }

            messagePropertyAttribute.ApplyingRules = true;
            foreach (IPropertyRule propertyRule in serializationDefaults.PropertyRules)
            {
                propertyRule.Check(this, serializationDefaults, classAttribute);
            }

            messagePropertyAttribute.ApplyingRules = false;
        }
Beispiel #14
0
        public MessageSerializedPropertyInfo(int index, PropertyInfo propertyInfo, MessagePropertyAttribute messagePropertyAttribute, SerializationDefaults serializationDefaults, MessageClassAttribute classAttribute)
        {
            Index        = index;
            PropertyInfo = propertyInfo;
            SetElementType(propertyInfo.PropertyType);
            ElementIsMessageSerializableObject = TypeIsMessageSerializableObject(ElementType);
            MessagePropertyAttribute           = messagePropertyAttribute;
            CalculatedFieldAttributes          = GetCalculatedFieldAttributes(propertyInfo);

            //TODO: throw new Exception("Probably need to change this to be called after all the MessageSerializedPropertyInfos are created and then pass the list in to CheckRules.  Also, since it's a member, a bunch of the parameters don't need to be passed in anyways");
            CheckRules(messagePropertyAttribute, propertyInfo, ElementType, ElementIsMessageSerializableObject, serializationDefaults, classAttribute);
        }