/// <summary> /// Resolves the contract converter. /// </summary> /// <param name="objectType">Type of the object.</param> /// <returns>JsonConverter.</returns> protected override JsonConverter ResolveContractConverter(Type objectType) { if (objectType.IsModelBase()) { return null; // pretend converter is not specified } return base.ResolveContractConverter(objectType); }
/// <summary> /// Determines whether this instance can convert the specified object type. /// </summary> /// <param name="objectType">Type of the object.</param> /// <returns><c>true</c> if this instance can convert the specified object type; otherwise, <c>false</c>.</returns> public override bool CanConvert(Type objectType) { var canConvert = objectType.IsModelBase(); return canConvert; }
/// <summary> /// Returns whether non-public reflection is allowed on the specified type. /// </summary> /// <param name="type">The type.</param> /// <returns><c>true</c> if non-public reflection is allowed, <c>false</c> otherwise.</returns> protected virtual bool AllowNonPublicReflection(Type type) { #if NET var allowNonPublicReflection = type.IsModelBase(); #else var allowNonPublicReflection = false; #endif return allowNonPublicReflection; }
/// <summary> /// Determines whether the specified type is serializable. /// </summary> /// <param name="type">The type.</param> /// <param name="serializerTypeInfo">The serializer type information.</param> /// <returns><c>true</c> if the specified type is serializable; otherwise, <c>false</c>.</returns> protected virtual bool IsTypeSerializable(Type type, XmlSerializerTypeInfo serializerTypeInfo) { if (type == null) { return false; } // DataContract attribute if (AttributeHelper.IsDecoratedWithAttribute<DataContractAttribute>(type)) { return true; } #if NET // Implements ISerializable if (type.ImplementsInterfaceEx<ISerializable>()) { return true; } #endif // Implements IXmlSerializer if (type.ImplementsInterfaceEx<System.Xml.Serialization.IXmlSerializable>()) { return true; } // Is ModelBase if (type.IsModelBase()) { return true; } // IsSerializable return type.IsSerializableEx(); }
private void AddTypeMembers(Type type, XmlSerializerTypeInfo serializerTypeInfo) { var typesToCheck = new List<Type>(); var isModelBase = type.IsModelBase(); if (isModelBase) { // No need to check members, they will be serialized by ModelBase //var catelTypeInfo = PropertyDataManager.Default.GetCatelTypeInfo(type); //var modelBaseProperties = catelTypeInfo.GetCatelProperties(); //foreach (var modelBaseProperty in modelBaseProperties) //{ // typesToCheck.Add(modelBaseProperty.Value.Type); //} } else { var allowNonPublicReflection = AllowNonPublicReflection(type); // Fields var fields = type.GetFieldsEx(BindingFlagsHelper.GetFinalBindingFlags(false, false, allowNonPublicReflection)); foreach (var field in fields) { typesToCheck.Add(field.FieldType); } // Properties var properties = type.GetPropertiesEx(BindingFlagsHelper.GetFinalBindingFlags(false, false, allowNonPublicReflection)); foreach (var property in properties) { typesToCheck.Add(property.PropertyType); } } foreach (var typeToCheck in typesToCheck) { if (serializerTypeInfo.IsTypeAlreadyHandled(typeToCheck)) { continue; } if (!IsTypeSerializable(typeToCheck, serializerTypeInfo)) { serializerTypeInfo.AddTypeAsHandled(typeToCheck); continue; } var propertyTypeFullName = typeToCheck.GetSafeFullName(); if (propertyTypeFullName == null) { serializerTypeInfo.AddTypeAsHandled(typeToCheck); continue; } GetKnownTypes(typeToCheck, serializerTypeInfo); } }
/// <summary> /// Gets the properties to serialize for the specified object. /// </summary> /// <param name="type">The type.</param> /// <returns>The list of properties to serialize.</returns> /// <exception cref="ArgumentNullException">The <paramref name="type"/> is <c>null</c>.</exception> public virtual Dictionary<string, MemberMetadata> GetRegularPropertiesToSerialize(Type type) { Argument.IsNotNull("type", type); return _regularPropertiesToSerializeCache.GetFromCacheOrFetch(type, () => { var serializableMembers = new Dictionary<string, MemberMetadata>(); var catelPropertyNames = new HashSet<string>(); var isModelBase = type.IsModelBase(); if (isModelBase) { catelPropertyNames = GetCatelPropertyNames(type, true); } var regularProperties = GetRegularProperties(type); foreach (var typeProperty in regularProperties) { var memberMetadata = typeProperty.Value; var propertyInfo = (PropertyInfo)memberMetadata.Tag; if (!catelPropertyNames.Contains(memberMetadata.MemberName)) { // If not a ModelBase, include by default var include = !isModelBase; if (AttributeHelper.IsDecoratedWithAttribute<IncludeInSerializationAttribute>(propertyInfo)) { include = true; } if (AttributeHelper.IsDecoratedWithAttribute<ExcludeFromSerializationAttribute>(propertyInfo)) { include = false; } if (include) { serializableMembers.Add(typeProperty.Key, memberMetadata); } } } return serializableMembers; }); }
/// <summary> /// Returns the sequence of properties of the specified type. /// </summary> /// <param name="type">The type.</param> /// <param name="schema">The schema.</param> /// <param name="schemaSet">The schema set.</param> /// <param name="serializationManager">The serialization manager.</param> /// <param name="exportedTypes">The exported types.</param> /// <returns>Sequence containing all properties.</returns> private static XmlSchemaSequence GetPropertiesSequence(Type type, XmlSchema schema, XmlSchemaSet schemaSet, ISerializationManager serializationManager, HashSet<string> exportedTypes) { Argument.IsNotNull("type", type); Argument.IsNotNull("schema", schema); Argument.IsNotNull("schemaSet", schemaSet); var propertiesSequence = new XmlSchemaSequence(); if (type.IsModelBase()) { var members = new List<MemberMetadata>(); members.AddRange(from field in serializationManager.GetFieldsToSerialize(type) select field.Value); members.AddRange(from property in serializationManager.GetCatelPropertiesToSerialize(type) select property.Value); members.AddRange(from property in serializationManager.GetRegularPropertiesToSerialize(type) select property.Value); foreach (var member in members) { var propertySchemaElement = new XmlSchemaElement(); propertySchemaElement.Name = member.MemberName; var memberType = member.MemberType; propertySchemaElement.IsNillable = memberType.IsNullableType(); propertySchemaElement.MinOccurs = 0; var exporter = new XsdDataContractExporter(schemaSet); var alreadyExported = IsAlreadyExported(schemaSet, memberType, exporter, exportedTypes); if (!alreadyExported) { if (!exportedTypes.Contains(memberType.FullName)) { exportedTypes.Add(memberType.FullName); } try { if (exporter.CanExport(memberType)) { exporter.Export(memberType); } } catch (Exception) { // Ignore } } propertySchemaElement.SchemaType = exporter.GetSchemaType(memberType); propertySchemaElement.SchemaTypeName = exporter.GetSchemaTypeName(memberType); propertiesSequence.Items.Add(propertySchemaElement); } } return propertiesSequence; }