コード例 #1
0
            public SerializerMemberDetails(ByteSerializerIndexSize indexSize, bool ignoreBinaryIndexAttribute, MemberDetail member)
            {
                this.indexSize = indexSize;
                this.ignoreBinaryIndexAttribute = ignoreBinaryIndexAttribute;

                this.Name     = member.Name;
                this.Type     = member.Type;
                this.CoreType = member.TypeDetail.CoreType;

                this.Getter = member.Getter;
                this.Setter = member.Setter;
            }
コード例 #2
0
ファイル: ByteSerializer.cs プロジェクト: szawaski/Zerra
        private static SerializerTypeDetails GetTypeInformation(Type type, ByteSerializerIndexSize indexSize, bool ignoreIndexAttribute)
        {
            var dictionarySetIndex = ((int)indexSize + 1) * (ignoreIndexAttribute ? 1 : 2);
            var dictionarySet      = typeInfoCache.GetOrAdd(dictionarySetIndex, (factoryKey) =>
            {
                return(new ConcurrentFactoryDictionary <Type, SerializerTypeDetails>());
            });
            var typeInfo = dictionarySet.GetOrAdd(type, (factoryKey) =>
            {
                return(new SerializerTypeDetails(indexSize, ignoreIndexAttribute, type));
            });

            return(typeInfo);
        }
コード例 #3
0
ファイル: ByteSerializer.cs プロジェクト: szawaski/Zerra
 /// <summary>
 /// Create serializer to convert objects to bytes and back.  Collections must be arrays or convertible from a List.  The deserializing instance must match the the specifications used to serialize.  When inlcuding type information, type safety is enforced so the deserialized types must be the same or inherit from the origional.
 /// </summary>
 /// <param name="propertyNames">Include property names in the serialization. Default false.</param>
 /// <param name="propertyTypes">Include type information for non-coretypes in the serialization. This allows the data to be deserialized without knowing the type beforehand and needed for properties that are boxed or an interface. Default false.</param>
 /// <param name="ignoreIndexAttribute">Ignore the index attribute marked on properties. Default false.</param>
 /// <param name="indexSize">Size of the property indexes. Use Byte unless the number of properties in an object exceeds 255. Default Byte.</param>
 /// <param name="encoding">The text encoder used for char and string.  Default System.Text.Encoding.Unicode.</param>
 public ByteSerializer(bool propertyNames = false, bool propertyTypes = false, bool ignoreIndexAttribute = false, ByteSerializerIndexSize indexSize = ByteSerializerIndexSize.Byte, Encoding encoding = null)
 {
     this.usePropertyNames     = propertyNames;
     this.includePropertyTypes = propertyTypes;
     this.ignoreIndexAttribute = ignoreIndexAttribute;
     this.indexSize            = indexSize;
     if (encoding != null)
     {
         this.encoding = encoding;
     }
     else
     {
         this.encoding = defaultEncoding;
     }
 }
コード例 #4
0
            public SerializerTypeDetails(ByteSerializerIndexSize indexSize, bool ignoreIndexAttribute, Type type)
            {
                this.indexSize            = indexSize;
                this.ignoreIndexAttribute = ignoreIndexAttribute;
                this.Type = type;

                this.TypeDetail = TypeAnalyzer.GetTypeDetail(type);

                var listTypeDetail = TypeAnalyzer.GetGenericTypeDetail(genericListType, type);
                var listCreator    = listTypeDetail.GetConstructor(typeof(int)).Creator;

                this.ListCreator = (length) => { return(listCreator(new object[] { length })); };

                if (!this.Type.IsEnum && !this.TypeDetail.CoreType.HasValue && !this.TypeDetail.SpecialType.HasValue && !this.TypeDetail.IsNullable && !this.TypeDetail.IsIEnumerableGeneric)
                {
                    var memberSets = new List <Tuple <MemberDetail, SerializerIndexAttribute, NonSerializedAttribute> >();
                    foreach (var member in this.TypeDetail.SerializableMemberDetails)
                    {
                        var indexAttribute         = member.Attributes.Select(x => x as SerializerIndexAttribute).Where(x => x != null).FirstOrDefault();
                        var nonSerializedAttribute = member.Attributes.Select(x => x as NonSerializedAttribute).Where(x => x != null).FirstOrDefault();
                        memberSets.Add(new Tuple <MemberDetail, SerializerIndexAttribute, NonSerializedAttribute>(member, indexAttribute, nonSerializedAttribute));
                    }

                    var indexProperties = new Dictionary <ushort, SerializerMemberDetails>();

                    if (!ignoreIndexAttribute)
                    {
                        var membersWithIndexes = memberSets.Where(x => x.Item2 != null && x.Item3 == null).ToArray();
                        if (membersWithIndexes.Length > 0)
                        {
                            foreach (var member in membersWithIndexes)
                            {
                                switch (indexSize)
                                {
                                case ByteSerializerIndexSize.Byte:
                                    if (member.Item2.Index > byte.MaxValue - IndexOffset)
                                    {
                                        throw new Exception("Index attribute too large for the index size");
                                    }
                                    break;

                                case ByteSerializerIndexSize.UInt16:
                                    if (member.Item2.Index > ushort.MaxValue - IndexOffset)
                                    {
                                        throw new Exception("Index attribute too large for the index size");
                                    }
                                    break;

                                default:
                                    throw new NotImplementedException();
                                }
                                ushort index = (ushort)(member.Item2.Index + IndexOffset);

                                indexProperties.Add(index, new SerializerMemberDetails(indexSize, ignoreIndexAttribute, member.Item1));
                            }
                        }
                    }

                    if (indexProperties.Count == 0)
                    {
                        int orderIndex = 0;
                        foreach (var member in memberSets.Where(x => x.Item3 == null))
                        {
                            switch (indexSize)
                            {
                            case ByteSerializerIndexSize.Byte:
                                if (orderIndex > byte.MaxValue - IndexOffset)
                                {
                                    throw new Exception("Index attribute too large for the index size");
                                }
                                break;

                            case ByteSerializerIndexSize.UInt16:
                                if (orderIndex > ushort.MaxValue - IndexOffset)
                                {
                                    throw new Exception("Index attribute too large for the index size");
                                }
                                break;

                            default:
                                throw new NotImplementedException();
                            }
                            ushort index = (ushort)(orderIndex + IndexOffset);

                            indexProperties.Add(index, new SerializerMemberDetails(indexSize, ignoreIndexAttribute, member.Item1));
                            orderIndex++;
                        }
                    }

                    this.IndexedProperties = indexProperties;
                }
            }