// // BASE-TYPE-LOOP // NON-ATTRIBUTE public static void UpdateEntityVariables <T>(T leftEntity, T rightEntity, VariableInfoDescriptor?leftVariablesDescriptor = null, VariableInfoDescriptor?rightVariablesDescriptor = null) where T : notnull { ensureNonNullEntityTypes(leftEntity, rightEntity, out var leftEntityType, out var rightEntityType); leftVariablesDescriptor = leftVariablesDescriptor ?? createDefaultVariableInfoSettings(); var leftEntityVariableMembers = leftEntityType.GetVariableMembers(descriptor: leftVariablesDescriptor); var intersectedEntityVariableMembers = leftEntityVariableMembers.Intersect(rightEntityType, rightVariablesDescriptor); updateEntityVariables(leftEntity, rightEntity, intersectedEntityVariableMembers); }
internal static VariableInfoDescriptor DefaultIfNull(this VariableInfoDescriptor?descriptor, bool seal) { descriptor = descriptor ?? new VariableInfoDescriptor(); if (seal) { descriptor.Seal(); } return(descriptor); }
public static IEnumerable <MemberInfo> GetFieldMembers(this Type type, VariableInfoDescriptor?descriptor = null) { descriptor = descriptor.DefaultIfNull(true); foreach (MemberInfo field in type.GetFields(descriptor.Flags)) { if (field.IsVariable(descriptor)) { yield return(field); } } }
public static MemberInfo?GetFieldMember(this Type type, string fieldName, VariableInfoDescriptor?descriptor = null) { descriptor = descriptor.DefaultIfNull(true); var member = type.GetMember(fieldName, MemberTypes.Field, descriptor.Flags).SingleOrDefault(); if (member == null || !member.IsVariable(descriptor)) { return(null); } return(member); }
public static IEnumerable <MemberInfo> GetPropertyMembers(this Type type, VariableInfoDescriptor?descriptor = null) { type = type ?? throw new ArgumentNullException(nameof(type)); descriptor = descriptor.DefaultIfNull(true); foreach (MemberInfo property in type.GetProperties(descriptor.Flags)) { if (property.IsVariable(descriptor)) { yield return(property); } } }
public static IEnumerable <MemberInfo> Intersect(this IEnumerable <MemberInfo> memberInfos, Type entityType, VariableInfoDescriptor?descriptor = null) => IEnumerableMemberInfoTools.Intersect(memberInfos, entityType, descriptor);
// ATTRIBUTE public static void UpdateEntityVariablesByAttribute <T>(T leftEntity, T rightEntity, Type attributeType, VariableInfoDescriptor?variableInfoSettings = null) where T : notnull { ensureNonNullEntityTypes(leftEntity, rightEntity, out var leftEntityType, out _); variableInfoSettings = variableInfoSettings ?? createDefaultVariableInfoSettings(); var leftEntityVariableMembers = leftEntityType.GetAttributeVariableMembers(attributeType, descriptor: variableInfoSettings) .Select(x => x.MemberInfo); UpdateEntityVariables(leftEntity, rightEntity, leftEntityVariableMembers); }
public static IEnumerable <MemberInfo> GetMembers(Func <Type, VariableInfoDescriptor, IEnumerable <MemberInfo> > getMembers, Type beginningType, Type?interruptingBaseType = null, VariableInfoDescriptor?variableInfoDescriptor = null) { if (beginningType is null) { yield break; } variableInfoDescriptor = variableInfoDescriptor.DefaultIfNull(true); if (variableInfoDescriptor.Flags.HasFlag(BindingFlags.DeclaredOnly)) { interruptingBaseType = beginningType.BaseType; } else { variableInfoDescriptor = variableInfoDescriptor.ShallowCopy(); variableInfoDescriptor.Flags |= BindingFlags.DeclaredOnly; variableInfoDescriptor.Seal(); interruptingBaseType = interruptingBaseType ?? typeof(object); } var basesTypes = beginningType.GetBaseTypes(interruptingBaseType); foreach (var type in basesTypes) { foreach (var varInfo in getMembers(type, variableInfoDescriptor)) { yield return(varInfo); } } }
// NON-TYPED public static IEnumerable <AttributeMemberInfo> GetAttributeMembers(Type attributeType, Func <Type, VariableInfoDescriptor, IEnumerable <MemberInfo> > getMembers, Type beginningType, Type?interruptingBaseType = null, VariableInfoDescriptor?variableInfoDescriptor = null, bool?getCustomAttributesInherit = null) { foreach (var type in beginningType.GetBaseTypes(interruptingBaseType)) { foreach (var propertyInfo in GetMembers(getMembers, beginningType, interruptingBaseType, variableInfoDescriptor)) { if (propertyInfo.TryGetAttributeVariableMember(attributeType, out var varAttrInfo, getCustomAttributesInherit) && !(varAttrInfo is null)) { yield return(varAttrInfo); } } } }
public static IEnumerable <AttributeMemberInfo> GetAttributePropertyMembers(this Type beginningType, Type attributeType, Type?interruptingBaseType = null, VariableInfoDescriptor?descriptor = null, bool?getCustomAttributesInherit = null) => ReflectionTools.GetAttributeMembers(attributeType, GetPropertyMembers, beginningType, interruptingBaseType, descriptor, getCustomAttributesInherit);
public static IEnumerable <AttributeMemberInfo <TAttribute> > GetAttributeFieldMembers <TAttribute>(this Type beginningType, Type?interruptingBaseType = null, VariableInfoDescriptor?descriptor = null, bool?getCustomAttributesInherit = null) where TAttribute : Attribute => ReflectionTools.GetAttributeMembers <TAttribute>(GetFieldMembers, beginningType, interruptingBaseType, descriptor, getCustomAttributesInherit);
public static IEnumerable <MemberInfo> GetPropertyMembers(this Type beginningType, Type interruptingBaseType, VariableInfoDescriptor?descriptor = null) => ReflectionTools.GetMembers((_type, __settings) => GetPropertyMembers(_type, __settings), beginningType, interruptingBaseType, descriptor);
// NON-TYPED public static AttributeMemberInfo?GetAttributePropertyMember(this Type type, Type attributeType, string propertyName, VariableInfoDescriptor?descriptor = null, bool?getCustomAttributesInherit = null) { descriptor = descriptor.DefaultIfNull(true); var property = GetPropertyMember(type, propertyName, descriptor); if (property == null) { return(null); } return(new AttributeMemberInfo(property, attributeType, getCustomAttributesInherit)); }
public static IEnumerable <MemberInfo> Intersect(IEnumerable <MemberInfo> memberInfos, Type entityType, VariableInfoDescriptor?descriptor = null) { memberInfos = memberInfos ?? throw new ArgumentNullException(nameof(memberInfos)); entityType = entityType ?? throw new ArgumentNullException(nameof(entityType)); descriptor = descriptor.DefaultIfNull(true); foreach (var memberInfo in memberInfos) { if (entityType.GetVariableMember(memberInfo.Name, descriptor) is null) { continue; } yield return(memberInfo); } }
// // // ORDERED // // BASE-TYPE-LOOP // TYPED /// <returns>Returns null if passed attribute allows multiple declarations.</returns> public static AttributeMemberInfo <TAttribute>[]? TryGetOrderedAttributeMemberInfos <TAttribute>(this Type type, Type?interruptingBaseType = null, VariableInfoDescriptor?descriptor = null, bool?getCustomAttributesInherit = null) where TAttribute : Attribute, IZeroBasedIndex { var customAttribute = typeof(TAttribute).GetCustomAttribute <AttributeUsageAttribute>(); if (customAttribute is null || customAttribute.AllowMultiple) { return(null); } var vars = GetAttributeVariableMembers <TAttribute>(type, interruptingBaseType, descriptor, getCustomAttributesInherit).ToList(); var array = new AttributeMemberInfo <TAttribute> [vars.Count]; foreach (var variable in vars) { array[variable.FirstAttribute().Index] = variable; } return(array); }
// NON-TYPED public static IEnumerable <AttributeMemberInfo> GetAttributeVariableMembers(this Type beginningType, Type attributeType, Type?interruptingBaseType = null, VariableInfoDescriptor?descriptor = null, bool?getCustomAttributesInherit = null) { descriptor = descriptor.DefaultIfNull(true); foreach (var variable in GetAttributeFieldMembers(beginningType, attributeType, interruptingBaseType, descriptor, getCustomAttributesInherit)) { yield return(variable); } foreach (var variable in GetAttributePropertyMembers(beginningType, attributeType, interruptingBaseType, descriptor, getCustomAttributesInherit)) { yield return(variable); } }
// NON-TYPED public static AttributeMemberInfo?GetAttributeVariableMember(this Type type, Type attributeType, string variableName, VariableInfoDescriptor?descriptor = null, bool?getCustomAttributesInherit = null) => GetAttributeFieldMember(type, attributeType, variableName, descriptor, getCustomAttributesInherit) ?? GetAttributePropertyMember(type, attributeType, variableName, descriptor, getCustomAttributesInherit);
public static IEnumerable <MemberInfo> GetVariableMembers(this Type beginningType, Type interruptingBaseType, VariableMemberTypes?variableMemberType = null, VariableInfoDescriptor?descriptor = null) { descriptor = descriptor.DefaultIfNull(true); if (variableMemberType == null || variableMemberType.Value.HasFlag(VariableMemberTypes.Field)) { foreach (var variable in GetFieldMembers(beginningType, interruptingBaseType, descriptor)) { yield return(variable); } } if (variableMemberType == null || variableMemberType.Value.HasFlag(VariableMemberTypes.Property)) { foreach (var variable in GetPropertyMembers(beginningType, interruptingBaseType, descriptor)) { yield return(variable); } } }
public static MemberInfo?GetVariableMember(this Type type, string variableName, VariableInfoDescriptor?descriptor = null) => GetPropertyMember(type, variableName, descriptor) ?? GetFieldMember(type, variableName, descriptor);
// NON-TYPED public static AttributeMemberInfo?GetAttributeFieldMember(this Type type, Type attributeType, string fieldName, VariableInfoDescriptor?descriptor = null, bool?getCustomAttributesInherit = null) { type = type ?? throw new ArgumentNullException(nameof(type)); descriptor = descriptor.DefaultIfNull(true); var field = GetFieldMember(type, fieldName, descriptor); if (field == null) { return(null); } return(new AttributeMemberInfo(field, attributeType, getCustomAttributesInherit)); }