コード例 #1
0
        private object SimpleSerializer(Type current)
        {
            if (TypeInitializerStatic.IsTypeRegistered(current))
            {
                return(Activator.CreateInstance(typeof(ModelsSerializer <>).MakeGenericType(current), new object[] { TypeInitializer }));
            }

            return(null);
        }
コード例 #2
0
        public override void Serialize(BsonSerializationContext context, BsonSerializationArgs args, T value)
        {
            var bsonWriter = context.Writer;

            if (value == null)
            {
                bsonWriter.WriteNull();
                return;
            }

            var entityTypeModel = this.CurentTypeModel;

            bsonWriter.WriteStartDocument();

            foreach (var prop in entityTypeModel.CurrentType.GetProperties())
            {
                object propertyValue = prop.GetValue(value);

                if (prop == entityTypeModel.IdProperty)
                {
                    if (propertyValue == null)
                    {
                        var id = this.GetId(entityTypeModel.IdProperty);

                        if (id != null)
                        {
                            entityTypeModel.IdProperty.SetValue(value, id);
                            bsonWriter.WriteName(MongoIdProperty);
                            BsonSerializer.LookupSerializer(entityTypeModel.IdProperty.PropertyType)
                            .Serialize(context, id);
                        }
                    }
                }
                else if (TypeInitializerStatic.GetTypeMetadata(prop.PropertyType) != null && propertyValue != null)
                {
                    var propTypeModel = TypeInitializerStatic.GetTypeMetadata(prop.PropertyType);
                    var idValue       = propTypeModel.IdProperty.GetValue(propertyValue);

                    if (idValue != null)
                    {
                        bsonWriter.WriteName(prop.GetNavigationPropertyName());
                        BsonSerializer.LookupSerializer(propTypeModel.IdProperty.PropertyType)
                        .Serialize(context, idValue);
                    }
                }
                else if (!prop.PropertyType.IsIEnumerableType() && propertyValue != null)
                {
                    bsonWriter.WriteName(prop.Name);
                    BsonSerializer.LookupSerializer(prop.PropertyType).Serialize(context, propertyValue);
                }
            }

            bsonWriter.WriteEndDocument();
        }
コード例 #3
0
        private object CollectionSerializer(Type current)
        {
            if (current.IsGenericType)
            {
                Type genericDefinition = current.GetGenericTypeDefinition();
                Type genericArgument   = current.GetGenericArguments()[0];

                if (TypeInitializerStatic.IsTypeRegistered(genericArgument))
                {
                    if (genericDefinition == typeof(ICollection <>))
                    {
                        return(Activator.CreateInstance(
                                   typeof(TrackingICollectionSerializer <>).MakeGenericType(genericArgument)));
                    }
                    else if (genericDefinition == typeof(IList <>))
                    {
                        return(Activator.CreateInstance(
                                   typeof(TrackingIListSerializer <>).MakeGenericType(genericArgument)));
                    }
                }
            }

            return(null);
        }
コード例 #4
0
 public ModelsSerializer() : base()
 {
     this.CurentTypeModel = TypeInitializerStatic.GetTypeMetadata(this.ValueType);
 }