private List <SerializedTypeDesc> GetDerivedTypesFromAttributes(MemberInfo member) { List <SerializedTypeDesc> derivedTypes = new List <SerializedTypeDesc>(); // XmlElementAttribute if (member.IsDefined(typeof(XmlElementAttribute), true)) { XmlElementAttribute[] xmlElemAttributes = (XmlElementAttribute[])Attribute.GetCustomAttributes(member, typeof(XmlElementAttribute)); foreach (XmlElementAttribute attr in xmlElemAttributes) { if (attr.Type != null) { SerializedTypeDesc attrSerTypeDesc = new SerializedTypeDesc { TypeHandle = attr.Type.TypeHandle }; if (!derivedTypes.Contains(attrSerTypeDesc)) { derivedTypes.Add(attrSerTypeDesc); } } } } return(derivedTypes); }
// We don't generate serialization code for well known types private bool IsWellKnownType(SerializedTypeDesc item) { Type itemType = item.Type; if ( itemType == typeof(Boolean) || itemType == typeof(Byte) || itemType == typeof(Char) || itemType == typeof(DateTime) || itemType == typeof(DateTimeOffset) || itemType == typeof(Decimal) || itemType == typeof(Double) || itemType == typeof(Guid) || itemType == typeof(Int16) || itemType == typeof(Int32) || itemType == typeof(Int64) || itemType == typeof(Object) || itemType == typeof(SByte) || itemType == typeof(Single) || itemType == typeof(String) || itemType == typeof(TimeSpan) || itemType == typeof(UInt16) || itemType == typeof(UInt32) || itemType == typeof(UInt64) ) { return(true); } return(false); }
// Main serialization routine private void SerializeType(SerializedTypeDesc item) { // Type embedded serialization Embedded_Serialization(item); // Type external serialization External_Serialization(item); }
private void ES_Normal(SerializedTypeDesc item) { ES_Constructor(item); ES_PolimorphicTypeEnumerations(item); ES_PolimorphicSerialization(item); ES_Serialization(item); ES_Deserialization(item); }
private void ES_PolimorphicSerialization(SerializedTypeDesc item) { foreach (var fieldName in item.DerivedTypes.Keys) { DerivedTypesDesc derivedTypesDesc = item.DerivedTypes[fieldName]; ES_FieldPolimorphicSerialization(fieldName, derivedTypesDesc); ES_FieldPolimorphicDeserialization(fieldName, derivedTypesDesc); } }
private void ES_Normal(SerializedTypeDesc item) { ES_Constructor(item); ES_CreateAccessorsForAllFields(item); ES_StaticInit(item); ES_PolimorphicTypeEnumerations(item); ES_PolimorphicSerialization(item); ES_Serialization(item); ES_Deserialization(item); }
private string ES_InheritanceChain(SerializedTypeDesc item) { // Normal object string result = "ComplexTypeSerializerBase, ICachedObjectSerializer<" + item.FullName + ">"; //// Array //if (item.Type.IsArray) // result = "ComplexTypeSerializerBase, ICachedObjectSerializer<" + item.FullName + ">"; ; return(result); }
private void ES_DeserializeNormalMember(SerializedTypeDesc item, FieldInfo field) { SerializedTypeDesc fieldItemTypeDesc = GetExistingTypeDesc(field.FieldType); if (IsWellKnownType(fieldItemTypeDesc)) { ES_DeserializeNormalWKTMember(item, field, fieldItemTypeDesc); } else { ES_DeserializeNormalObjectMember(item, field, fieldItemTypeDesc); } }
private void ES_DeserializeNormalArrayMember(SerializedTypeDesc item, FieldInfo field, SerializedTypeDesc fieldItemTypeDesc) { SerializedTypeDesc elemItemTypeDesc = GetExistingTypeDesc(field.FieldType.GetElementType()); if (IsWellKnownType(elemItemTypeDesc)) { ES_DeserializeNormalWKTArrayMember(item, field, fieldItemTypeDesc, elemItemTypeDesc); } else { ES_DeserializeNormalObjectArrayMember(item, field, fieldItemTypeDesc, elemItemTypeDesc); } }
private void ES_SerializeNormalArrayMember(SerializedTypeDesc item, MemberInfo member, SerializedTypeDesc fieldItemTypeDesc) { SerializedTypeDesc elemItemTypeDesc = GetExistingTypeDesc(GetMemberType(member).GetElementType()); if (IsWellKnownType(elemItemTypeDesc)) { ES_SerializeNormalWKTArrayMember(item, member, fieldItemTypeDesc, elemItemTypeDesc); } else { ES_SerializeNormalObjectArrayMember(item, member, fieldItemTypeDesc, elemItemTypeDesc); } }
private void ES_SerializeNormalMember(SerializedTypeDesc item, MemberInfo member) { SerializedTypeDesc fieldItemTypeDesc = GetExistingTypeDesc(GetMemberType(member)); if (IsWellKnownType(fieldItemTypeDesc)) { ES_SerializeNormalWKTMember(item, member, fieldItemTypeDesc); } else { ES_SerializeNormalObjectMember(item, member, fieldItemTypeDesc); } }
private SerializedTypeDesc GetExistingTypeDesc(Type type) { SerializedTypeDesc typeDesc = new SerializedTypeDesc { TypeHandle = type.TypeHandle }; if (SerializedTypes.ContainsKey(typeDesc.SafeFullName)) { return(SerializedTypes[typeDesc.SafeFullName]); } SerializedTypes.Add(typeDesc.SafeFullName, typeDesc); return(typeDesc); }
private void ES_CreateAccessorsForAllFields(SerializedTypeDesc item) { NewLine(); WriteLine("#region Accessors for all the fields"); NewLine(); // Member fields inspection FieldInfo[] serializableMembers = (FieldInfo[])GetSerializableMembers(item.Type); Array.ForEach(serializableMembers, field => { ES_CreateAccessor4Field(item, field); }); NewLine(); WriteLine("#endregion"); NewLine(); }
private void ES_PolimorphicTypeEnumerations(SerializedTypeDesc item) { foreach (var fieldName in item.DerivedTypes.Keys) { NewLine(); DerivedTypesDesc derivedTypesDesc = item.DerivedTypes[fieldName]; WriteLine("// Possible derived types to store on field " + fieldName + " - base type is " + derivedTypesDesc.BaseType.FullName); Write("public enum " + fieldName + "_SerializedTypeId { TypeIs_" + derivedTypesDesc.BaseType.SafeFullName); SerializedTypeDesc[] derivedTypes = derivedTypesDesc.DerivedTypes; foreach (var derType in derivedTypes) { Write(", TypeIs_" + derType.SafeFullName); } WriteLine(" }"); } }
private void Embedded_Serialization(SerializedTypeDesc item) { // We don't generate serializer for array types and well know types if (item.Type.IsArray || IsWellKnownType(item)) { return; } // Separate file StartNewFile(item.SafeFullName + "_EmbeddedSerializer"); Embedded_Header(item); this.PushIndent(); this.PushIndent(); ES_Normal(item); this.PopIndent(); this.PopIndent(); Embedded_Footer(item); Manager.RestorePreviousBlock(); }
// Walks through all the fields and gather types for subtype also private void GatherAllUsedTypesForType(Type type) { // Preprocess full name only SerializedTypeDesc typeDesc = new SerializedTypeDesc { TypeHandle = type.TypeHandle }; // We analyse type once only ... if (SerializedTypes.ContainsKey(typeDesc.SafeFullName)) { return; } // Type registration typeDesc = GetExistingTypeDesc(type); // Member fields inspection FieldInfo[] serializableMembers = (FieldInfo[])GetSerializableMembers(type); Array.ForEach(serializableMembers, field => { GatherAllUsedTypesForType(field.FieldType); if (field.FieldType.IsArray) { GatherAllUsedTypesForType(field.FieldType.GetElementType()); } }); // Does it have any serialization attributes? Type[] otherTypes = GetTypesFromSerializationAttributesOfTypeMembers(type); Array.ForEach(otherTypes, otherType => { GatherAllUsedTypesForType(otherType); if (otherType.IsArray) { GatherAllUsedTypesForType(otherType.GetElementType()); } }); }
private void GetDerivedTypesFromPropertyAttributes(List <SerializedTypeDesc> derivedTypes, Type baseType) { // Unknown derived types List <SerializedTypeDesc> typesToAdd = new List <SerializedTypeDesc>(); // For all registered types and for all available fields foreach (SerializedTypeDesc item in SerializedTypes.Values) { List <MemberInfo> elementsToInspect = new List <MemberInfo>(); // Fields elementsToInspect.AddRange(item.Type.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.FlattenHierarchy)); // Properties elementsToInspect.AddRange(item.Type.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.FlattenHierarchy)); // Inspection elementsToInspect.ForEach(elem => { // XmlElementAttribute if (elem.IsDefined(typeof(XmlElementAttribute), true)) { XmlElementAttribute[] xmlElemAttributes = (XmlElementAttribute[])Attribute.GetCustomAttributes(elem, typeof(XmlElementAttribute)); // Is it FieldInfo? if (elem.MemberType == MemberTypes.Field) { FieldInfo fieldInfo = (FieldInfo)elem; Type fieldType = fieldInfo.FieldType; if (fieldType.IsArray) { fieldType = fieldType.GetElementType(); } if (fieldType == baseType) { foreach (XmlElementAttribute attr in xmlElemAttributes) { if (attr.Type != null) { SerializedTypeDesc attrSerTypeDesc = new SerializedTypeDesc { TypeHandle = attr.Type.TypeHandle }; if (!derivedTypes.Contains(attrSerTypeDesc)) { typesToAdd.Add(attrSerTypeDesc); } } } } } // Is it PropertyInfo? if (elem.MemberType == MemberTypes.Property) { PropertyInfo propertyInfo = (PropertyInfo)elem; Type propType = propertyInfo.PropertyType; if (propType.IsArray) { propType = propType.GetElementType(); } if (propType == baseType) { foreach (XmlElementAttribute attr in xmlElemAttributes) { if (attr.Type != null) { SerializedTypeDesc attrSerTypeDesc = new SerializedTypeDesc { TypeHandle = attr.Type.TypeHandle }; if (!derivedTypes.Contains(attrSerTypeDesc)) { typesToAdd.Add(attrSerTypeDesc); } } } } } } }); } // Add new types typesToAdd.ForEach(elem => { GetExistingTypeDesc(elem.Type); }); typesToAdd.ForEach(elem => { if (!derivedTypes.Contains(elem)) { derivedTypes.Add(GetExistingTypeDesc(elem.Type)); } }); }
private void ES_SerializeMembers(SerializedTypeDesc item) { if (item.Name == "ePCR_DocumentPatientInfoPatientHistoryAllergies") { int k = 0; } // List of all the members we can serialize List <MemberInfo> serializableMembers = new List <MemberInfo>((MemberInfo[])GetSerializableMembers(item.Type)); // Serialize normal fields List <MemberInfo> normalMembers = new List <MemberInfo>(); normalMembers.AddRange(serializableMembers.Where(info => { // Is it array? if (MemberTypeIsArray(info)) { return(false); } // Is it polymorphic type field? if (item.DerivedTypes.ContainsKey(info.Name)) { return(false); } return(true); })); normalMembers.ForEach(member => ES_SerializeNormalMember(item, member)); // Serialize polymorphic fields List <MemberInfo> pilimorhicMembers = new List <MemberInfo>(); pilimorhicMembers.AddRange(serializableMembers.Where(info => { // Is it array? if (MemberTypeIsArray(info)) { return(false); } // Is it polymorphic type field? if (item.DerivedTypes.ContainsKey(info.Name)) { return(true); } return(false); })); pilimorhicMembers.ForEach(member => { SerializedTypeDesc memberItemTypeDesc = GetExistingTypeDesc(GetMemberType(member)); ES_SerializePolimorphicMember(item, member, memberItemTypeDesc); }); // Serialize normal array fields List <MemberInfo> normalArrayMembers = new List <MemberInfo>(); normalArrayMembers.AddRange(serializableMembers.Where(info => { // Is itn't array? if (!MemberTypeIsArray(info)) { return(false); } // Is it polymorphic type field? if (item.DerivedTypes.ContainsKey(info.Name)) { return(false); } return(true); })); normalArrayMembers.ForEach(member => { SerializedTypeDesc fieldItemTypeDesc = GetExistingTypeDesc(GetMemberType(member)); ES_SerializeNormalArrayMember(item, member, fieldItemTypeDesc); }); // Serialize polimorphic arrays List <MemberInfo> polimorphicArrayFields = new List <MemberInfo>(); polimorphicArrayFields.AddRange(serializableMembers.Where(info => { // Is itn't array? if (!MemberTypeIsArray(info)) { return(false); } // Is it polymorphic type field? if (item.DerivedTypes.ContainsKey(info.Name)) { return(true); } return(false); })); polimorphicArrayFields.ForEach(member => { SerializedTypeDesc fieldItemTypeDesc = GetExistingTypeDesc(GetMemberType(member)); SerializedTypeDesc elemItemTypeDesc = GetExistingTypeDesc(GetMemberType(member).GetElementType()); ES_SerializePolymorphicArrayMember(item, member, fieldItemTypeDesc, elemItemTypeDesc); }); }
// We don't generate serialization code for well known types private bool IsWellKnownType(SerializedTypeDesc item) { Type itemType = item.Type; return(IsWellKnownType(itemType)); }
private void ES_DeserializeMembers(SerializedTypeDesc item) { // List of all the members we can serialize List <FieldInfo> serializableMembers = new List <FieldInfo>((FieldInfo[])GetSerializableMembers(item.Type)); // Serialize normal fields List <FieldInfo> normalFields = new List <FieldInfo>(); normalFields.AddRange(serializableMembers.Where(info => { // Is it array? if (info.FieldType.IsArray) { return(false); } // Is it polymorphic type field? if (item.DerivedTypes.ContainsKey(info.Name)) { return(false); } return(true); })); normalFields.ForEach(field => ES_DeserializeNormalMember(item, field)); // Serialize polymorphic fields List <FieldInfo> pilimorhicFields = new List <FieldInfo>(); pilimorhicFields.AddRange(serializableMembers.Where(info => { // Is it array? if (info.FieldType.IsArray) { return(false); } // Is it polymorphic type field? if (item.DerivedTypes.ContainsKey(info.Name)) { return(true); } return(false); })); pilimorhicFields.ForEach(field => { SerializedTypeDesc fieldItemTypeDesc = GetExistingTypeDesc(field.FieldType); ES_DeserializePolymorphicMember(item, field, fieldItemTypeDesc); }); // Serialize normal array fields List <FieldInfo> normalArrayFields = new List <FieldInfo>(); normalArrayFields.AddRange(serializableMembers.Where(info => { // Is itn't array? if (!info.FieldType.IsArray) { return(false); } // Is it polymorphic type field? if (item.DerivedTypes.ContainsKey(info.Name)) { return(false); } return(true); })); normalArrayFields.ForEach(field => { SerializedTypeDesc fieldItemTypeDesc = GetExistingTypeDesc(field.FieldType); ES_DeserializeNormalArrayMember(item, field, fieldItemTypeDesc); }); // Serialize polimorphic arrays List <FieldInfo> polimorphicArrayFields = new List <FieldInfo>(); polimorphicArrayFields.AddRange(serializableMembers.Where(info => { // Is itn't array? if (!info.FieldType.IsArray) { return(false); } // Is it polymorphic type field? if (item.DerivedTypes.ContainsKey(info.Name)) { return(true); } return(false); })); polimorphicArrayFields.ForEach(field => { SerializedTypeDesc fieldItemTypeDesc = GetExistingTypeDesc(field.FieldType); SerializedTypeDesc elemItemTypeDesc = GetExistingTypeDesc(field.FieldType.GetElementType()); ES_DeserializePolymorphicArrayMember(item, field, fieldItemTypeDesc, elemItemTypeDesc); }); }