Ejemplo n.º 1
0
        public static Graph Convert(Graph graph, Type type)
        {
            var   constructor  = TypeAnalyzer.GetGenericTypeDetail(typeof(Graph <>), type).GetConstructor(new Type[] { typeof(Graph) });
            Graph genericGraph = (Graph)constructor.Creator(new object[] { graph });

            return(genericGraph);
        }
            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;
                }
            }
Ejemplo n.º 3
0
        public object Bind(Type type)
        {
            if (IsNull)
            {
                return(null);
            }

            var typeDetail = TypeAnalyzer.GetTypeDetail(type);

            if (typeDetail.IsIEnumerableGeneric)
            {
                if (typeDetail.SpecialType == SpecialType.Dictionary)
                {
                    if (jsonType != JsonObjectType.Object)
                    {
                        throw new InvalidCastException();
                    }
                    var dictionary = (IDictionary)typeDetail.Creator();
                    foreach (var item in valueProperties)
                    {
                        var key   = TypeAnalyzer.Convert(item.Key, typeDetail.IEnumerableGenericInnerTypeDetails.InnerTypes[0]);
                        var value = item.Value.Bind(typeDetail.IEnumerableGenericInnerTypeDetails.InnerTypes[1]);
                        dictionary.Add(key, value);
                    }
                    return(dictionary);
                }
                else if (typeDetail.Type == typeof(byte[]))
                {
                    if (jsonType != JsonObjectType.String)
                    {
                        throw new InvalidCastException();
                    }
                    if (valueString == null)
                    {
                        return(null);
                    }
                    return(Convert.FromBase64String(valueString));
                }
                else
                {
                    if (jsonType != JsonObjectType.Array)
                    {
                        throw new InvalidCastException();
                    }
                    var innerType = typeDetail.InnerTypeDetails[0].Type;
                    if (!typeDetail.Type.IsArray && typeDetail.IsIList)
                    {
                        var listType = TypeAnalyzer.GetGenericTypeDetail(plainListType, innerType);
                        var list     = (IList)listType.Creator();
                        foreach (var item in valueArray)
                        {
                            var value = item.Bind(innerType);
                            list.Add(value);
                        }
                        return(list);
                    }
                    else
                    {
                        var array = Array.CreateInstance(innerType, valueArray.Length);
                        for (var i = 0; i < valueArray.Length; i++)
                        {
                            var value = valueArray[i].Bind(innerType);
                            array.SetValue(value, i);
                        }
                        return(array);
                    }
                }
            }

            if (typeDetail.CoreType.HasValue)
            {
                if (jsonType != JsonObjectType.String && jsonType != JsonObjectType.Literal)
                {
                    throw new InvalidCastException();
                }
                return(typeDetail.CoreType.Value switch
                {
                    CoreType.Boolean => (bool)this,
                    CoreType.Byte => (byte)this,
                    CoreType.SByte => (sbyte)this,
                    CoreType.Int16 => (short)this,
                    CoreType.UInt16 => (ushort)this,
                    CoreType.Int32 => (int)this,
                    CoreType.UInt32 => (uint)this,
                    CoreType.Int64 => (long)this,
                    CoreType.UInt64 => (ulong)this,
                    CoreType.Single => (float)this,
                    CoreType.Double => (double)this,
                    CoreType.Decimal => (decimal)this,
                    CoreType.Char => (char)this,
                    CoreType.DateTime => (DateTime)this,
                    CoreType.DateTimeOffset => (DateTimeOffset)this,
                    CoreType.TimeSpan => (TimeSpan)this,
                    CoreType.Guid => (Guid)this,
                    CoreType.String => (string)this,
                    CoreType.BooleanNullable => (bool?)this,
                    CoreType.ByteNullable => (byte?)this,
                    CoreType.SByteNullable => (sbyte?)this,
                    CoreType.Int16Nullable => (short?)this,
                    CoreType.UInt16Nullable => (ushort?)this,
                    CoreType.Int32Nullable => (int?)this,
                    CoreType.UInt32Nullable => (uint?)this,
                    CoreType.Int64Nullable => (long?)this,
                    CoreType.UInt64Nullable => (ulong?)this,
                    CoreType.SingleNullable => (float?)this,
                    CoreType.DoubleNullable => (double?)this,
                    CoreType.DecimalNullable => (decimal?)this,
                    CoreType.CharNullable => (char?)this,
                    CoreType.DateTimeNullable => (DateTime?)this,
                    CoreType.DateTimeOffsetNullable => (DateTimeOffset?)this,
                    CoreType.TimeSpanNullable => (TimeSpan?)this,
                    CoreType.GuidNullable => (Guid?)this,
                    _ => throw new NotImplementedException(),
                });