void SetMemberAttributeFlags(ref MemberMetadata metadata) { foreach (var attribute in metadata.Info.GetCustomAttributes(true)) { if (attribute is ConfigMandatoryAttribute) { metadata.HasConfigMandatoryAttribute = true; } else if (attribute is ConfigAllowMissingAttribute) { metadata.HasConfigAllowMissingAttribute = true; } else if (attribute is ConfigIgnoreAttribute) { metadata.HasConfigIgnoreAttribute = true; } } }
private static IType GetType( IEntityPersister currentEntityPersister, IType currentType, MemberMetadata member, ISessionFactoryImplementor sessionFactory) { // Not mapped if (currentType == null) { return(null); } IEntityPersister persister; if (!member.HasIndexer || currentEntityPersister == null) { if (member.ConvertType == null) { return(currentType); // q.Prop, q.OneToManyCompositeElement[0].Prop } return(TryGetEntityPersister(member.ConvertType, sessionFactory, out persister) ? persister.EntityMetamodel.EntityType // (Entity)q.Prop, (Entity)q.OneToManyCompositeElement[0].Prop : TypeFactory.GetDefaultTypeFor(member.ConvertType)); // (long)q.Prop, (long)q.OneToManyCompositeElement[0].Prop } if (!(currentType is IAssociationType associationType)) { // q.Prop[0] return(null); } var queryableCollection = (IQueryableCollection)associationType.GetAssociatedJoinable(sessionFactory); if (member.ConvertType == null) { // q.OneToMany[0] return(queryableCollection.ElementType); } return(TryGetEntityPersister(member.ConvertType, sessionFactory, out persister) ? persister.EntityMetamodel.EntityType // (Entity)q.OneToMany[0] : TypeFactory.GetDefaultTypeFor(member.ConvertType)); // (long)q.OneToMany[0] }
public static T GetPrivateMember <T>(this object obj, MemberMetadata metadata) { var flags = BindingFlags.NonPublic; T retval; if (!metadata.IsStatic) { flags |= BindingFlags.Instance; } if (metadata.IsProperty) { var propertyInfo = obj.GetType().GetProperty(metadata.MemberName, flags); if (propertyInfo == null) { return(default(T)); } if (!propertyInfo.CanRead) { return(default(T)); } if (propertyInfo.GetIndexParameters().Any()) { return(default(T)); } retval = (T)propertyInfo.GetValue(metadata.IsStatic ? null : obj, null); } else { var fieldInfo = obj.GetType().GetField(metadata.MemberName, flags); if (fieldInfo == null) { return(default(T)); } retval = (T)fieldInfo.GetValue(metadata.IsStatic ? null : obj); } return(retval); }
public static void SetPrivateMember <T>(this object obj, MemberMetadata metadata, T value) { var flags = BindingFlags.NonPublic; if (!metadata.IsStatic) { flags |= BindingFlags.Instance; } if (metadata.IsProperty) { var propertyInfo = obj.GetType().GetProperty(metadata.MemberName, flags); if (propertyInfo == null) { return; } if (!propertyInfo.CanWrite) { return; } if (propertyInfo.GetIndexParameters().Any()) { return; } propertyInfo.SetValue(metadata.IsStatic ? null : obj, value, null); } else { var fieldInfo = obj.GetType().GetField(metadata.MemberName, flags); if (fieldInfo == null) { return; } fieldInfo.SetValue(metadata.IsStatic ? null : obj, value); } }
public MemberInfoBasedSerializationContext([NotNull] MemberInfo memberInfoContext) { if (memberInfoContext == null) { throw new ArgumentNullException(nameof(memberInfoContext), $"Provided member {nameof(memberInfoContext)} is null."); } //From the member info we can grab the Type and the context related to it (if any) TargetType = memberInfoContext.Type(); //we should search for known metadata; ignore stuff like WireMember. It doesn't affect serialization of the Type. Just order. MemberMetadata = memberInfoContext.GetCustomAttributes <Attribute>(false) .Where(IsContextualMemberAttribute); //Now we build the metadata for the Type itself. This could be some additional serialization information or it could be //Information about potential subtypes TypeMetadata = memberInfoContext.GetCustomAttributes <Attribute>(false) .Where(IsContextualTypeAttribute); //If there is any metadata in the member metdata then this serialization will require context. ContextRequirement = MemberMetadata.Any() ? SerializationContextRequirement.RequiresContext : SerializationContextRequirement.Contextless; }
public void SaveTo(KafkaWriter writer) { writer.Write(MemberId); writer.Write(ClientId); writer.Write(ClientHost); //writer.Write(MemberMetadata); //writer.Write(MemberAssignment); if (MemberMetadata == null) { writer.Write((Byte[])null); } else { MemberMetadata.SaveTo(writer); } if (MemberAssignment == null) { writer.Write((Byte[])null); } else { MemberAssignment.SaveTo(writer); } }
private static bool TraverseMembers( ISessionFactoryImplementor sessionFactory, Stack <MemberMetadata> memberPaths, IEntityPersister currentEntityPersister, out IType mappedType, out IEntityPersister entityPersister, out IAbstractComponentType component, out string memberPath) { // Traverse the members that were traversed by the TryGetAllMemberMetadata method in the reverse order and try to keep // tracking the entity persister until all members are traversed. var member = memberPaths.Pop(); var currentType = currentEntityPersister.EntityMetamodel.GetPropertyType(member.Path); IAbstractComponentType currentComponentType = null; while (memberPaths.Count > 0 && currentType != null) { memberPath = member.Path; var convertType = member.ConvertType; member = memberPaths.Pop(); switch (currentType) { case IAssociationType associationType: ProcessAssociationType( associationType, sessionFactory, member, convertType, out currentType, out currentEntityPersister, out currentComponentType); break; case IAbstractComponentType componentType: currentComponentType = componentType; if (currentEntityPersister == null) { // When persister is not available (q.OneToManyCompositeElement[0].Prop), try to get the type from the component currentType = TryGetComponentPropertyType(componentType, member.Path); } else { // Concatenate the component property path in order to be able to use EntityMetamodel.GetPropertyType to retrieve the type. // As GetPropertyType supports only components, do not concatenate when dealing with collection composite elements or elements. // q.Component.Prop member = new MemberMetadata( $"{memberPath}.{member.Path}", member.ConvertType, member.HasIndexer); // q.Component.Prop currentType = currentEntityPersister.EntityMetamodel.GetPropertyType(member.Path); } break; default: // q.Prop.NotMappedProp currentType = null; currentEntityPersister = null; currentComponentType = null; break; } } // When traversed to the top of the expression, return the current tracking values if (memberPaths.Count == 0) { memberPath = currentEntityPersister != null || currentComponentType != null ? member.Path : null; mappedType = GetType(currentEntityPersister, currentType, member, sessionFactory); entityPersister = currentEntityPersister; component = currentComponentType; return(mappedType != null); } // Member not mapped memberPath = null; mappedType = null; entityPersister = null; component = null; return(false); }
/// <summary> /// Creates an instance of <see cref="SkippedMemberMetadataSerializer"/> /// </summary> /// <param name="memberMetadata">An instance of <see cref="MemberMetadata"/> to use</param> public SkippedMemberMetadataSerializer(MemberMetadata memberMetadata) : base(memberMetadata) { this._skippedBytes = this._memberMetadata.Capacity; }
//////////////////////////////////////////// TypeInfo CacheTypeInfo(Type type) { var info = new TypeInfo { FromDoc = type.GetMethod("FromDoc", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static), PostDoc = type.GetMethod("PostDoc", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static) }; // Read class attributes foreach (var attribute in type.GetCustomAttributes(true)) { switch (attribute) { case ConfigMandatoryAttribute _: info.AttributeFlags |= ClassAttributesFlags.HasConfigMandatoryAttribute; break; case ConfigAllowMissingAttribute _: info.AttributeFlags |= ClassAttributesFlags.HasConfigAllowMissingAttribute; break; } } Configs.Assert(info.AttributeFlags != ClassAttributesFlags.Invalid, $"Type {type.Name} has both ConfigAllowMissing and ConfigMandatory attributes."); var memberBindingFlags = BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static; var properties = type.GetProperties(memberBindingFlags); var fields = type.GetFields(memberBindingFlags); // Count the members. int memberCount = 0; foreach (var propertyInfo in properties) { if (!propertyInfo.IsSpecialName && propertyInfo.CanWrite && propertyInfo.CanRead) { memberCount++; } } foreach (var fieldInfo in fields) { if (!fieldInfo.IsSpecialName && fieldInfo.Name[0] != '<') { memberCount++; } } info.Members = new MemberMetadata[memberCount]; int currentMemberIndex = 0; // Read all properties from the type. foreach (var propertyInfo in properties) { if (propertyInfo.IsSpecialName || !propertyInfo.CanWrite || !propertyInfo.CanRead) { continue; } var metadata = new MemberMetadata { Info = propertyInfo, ShortName = RemoveHungarianPrefix(propertyInfo.Name), IsField = false, Type = propertyInfo.PropertyType }; SetMemberAttributeFlags(ref metadata); info.Members[currentMemberIndex] = metadata; currentMemberIndex++; } // Read all fields from the type. foreach (var fieldInfo in fields) { // Compiler-generated property backing fields have the name "<propertyName>k_BackingField" so // ignore any fields with names that start with '<'. Apparently IsSpecialName doesn't cover // this case. if (fieldInfo.IsSpecialName || fieldInfo.Name[0] == '<') { continue; } var metadata = new MemberMetadata { Info = fieldInfo, ShortName = RemoveHungarianPrefix(fieldInfo.Name), IsField = true, Type = fieldInfo.FieldType }; SetMemberAttributeFlags(ref metadata); info.Members[currentMemberIndex] = metadata; currentMemberIndex++; } cachedTypeInfo[type] = info; return(info); }
private static bool TraverseMembers( ISessionFactoryImplementor sessionFactory, Stack <MemberMetadata> memberPaths, IEntityPersister currentEntityPersister, out IType mappedType, out IEntityPersister entityPersister, out IAbstractComponentType component, out string memberPath) { // Traverse the members that were traversed by the TryGetAllMemberMetadata method in the reverse order and try to keep // tracking the entity persister until all members are traversed. var member = memberPaths.Pop(); var currentType = GetPropertyType(currentEntityPersister, member.Path); IAbstractComponentType currentComponentType = null; while (memberPaths.Count > 0 && currentType != null) { memberPath = member.Path; var convertType = member.ConvertType; member = memberPaths.Pop(); switch (currentType) { case IAssociationType associationType: ProcessAssociationType( associationType, sessionFactory, member, convertType, out currentType, out currentEntityPersister, out currentComponentType); break; case IAbstractComponentType componentType: currentComponentType = componentType; currentType = TryGetComponentPropertyType(componentType, member.Path); if (currentEntityPersister != null) { // q.Component.Prop member = new MemberMetadata( memberPath + "." + member.Path, member.ConvertType, member.HasIndexer); } break; default: // q.Prop.NotMappedProp currentType = null; currentEntityPersister = null; currentComponentType = null; break; } } // When traversed to the top of the expression, return the current tracking values if (memberPaths.Count == 0) { memberPath = currentEntityPersister != null || currentComponentType != null ? member.Path : null; mappedType = GetType(currentEntityPersister, currentType, member, sessionFactory); entityPersister = currentEntityPersister; component = currentComponentType; return(mappedType != null); } // Member not mapped memberPath = null; mappedType = null; entityPersister = null; component = null; return(false); }
public MemberMetadataSerializer(MemberMetadata member) : base(member) { this._memberInfo = member.MemberInfo as IMemberInfo <TValue>; this._serializer = member.SuccessorMetadata.Serializer as ISerializer <TValue>; }
/// <summary> /// Creates an instance of <see cref="MemberMetadataSerializer"/> /// </summary> /// <param name="memberMetadata">An instance of <see cref="MemberMetadata"/> to use</param> public MemberMetadataSerializer(MemberMetadata memberMetadata) { this._memberMetadata = memberMetadata; }