private static ExtendableDtoMetadata Build(Type extendableDtoType) { Assumption.AssertNotNull(extendableDtoType, nameof(extendableDtoType)); ExtendableDtoMetadata result = new ExtendableDtoMetadata(); result.ExtendableDtoType = extendableDtoType; List <Type> reservedPropertiesNestedTypes = new List <Type>(); Type extendableDtoHierarchyType = extendableDtoType; while (extendableDtoHierarchyType != null) { Type reservedPropertiesNestedType = ReflectionUtility.GetNestedTypeByName( extendableDtoHierarchyType, ExtendableDtoBase.reservedPropertiesNestedTypeName, BindingFlags.Public | BindingFlags.Static ); if (reservedPropertiesNestedType != null) { reservedPropertiesNestedTypes.Add(reservedPropertiesNestedType); } if (extendableDtoHierarchyType.BaseType == typeof(ExtendableDtoBase)) { break; } if (extendableDtoHierarchyType.BaseType != null) { extendableDtoHierarchyType = extendableDtoHierarchyType.BaseType; } } List <FieldInfo> reservedAttributes = new List <FieldInfo>(); foreach (Type reservedPropertiesNestedType in reservedPropertiesNestedTypes) { reservedAttributes.AddRange( ReflectionUtility.GetAllPublicStaticFields(reservedPropertiesNestedType) ); } Dictionary <string, PropertyInfo> reservedPropertyInfoByName = new Dictionary <string, PropertyInfo>(reservedAttributes.Count); result.ReservedPropertyInfoByReservedKey = reservedPropertyInfoByName; foreach (var reservedAttribue in reservedAttributes) { var property = extendableDtoType.GetProperty(reservedAttribue.Name, BindingFlags.Public | BindingFlags.Instance | BindingFlags.FlattenHierarchy); Assumption.AssertNotNull(property, nameof(property)); string reservedKey = ReflectionUtility.GetStaticFieldValue <string>(reservedAttribue); Assumption.AssertNotNullOrWhiteSpace(reservedKey, nameof(reservedKey)); reservedPropertyInfoByName.Add(reservedKey, property); } return(result); }
/// <summary> /// Initializes a new instance of the <see cref="ExtendableDtoBase"/> class. /// </summary> /// <param name="arbitraryKeyValuePairs">The arbitrary key value pairs.</param> protected ExtendableDtoBase(IDictionary <string, object> arbitraryKeyValuePairs) { this._metadata = ExtendableDtoBase.metadataByDerivedType[this.GetType()]; Assumption.AssertNotNull(this._metadata, nameof(this._metadata)); if (arbitraryKeyValuePairs != null) { foreach (var key in arbitraryKeyValuePairs.Keys) { this[key] = arbitraryKeyValuePairs[key]; } } }
private static ExtendableDtoMetadata Build(Type extendableDtoType) { ExtendableDtoMetadata result = new ExtendableDtoMetadata(); result.ExtendableDtoType = extendableDtoType; Type reservedPropertiesNestedType = ReflectionUtil.GetNestedTypeByName( extendableDtoType, ExtendableDtoBase.reservedPropertiesNestedTypeName, BindingFlags.Public | BindingFlags.Static ); Assumption.AssertNotNull(reservedPropertiesNestedType, nameof(reservedPropertiesNestedType)); var reservedAttributes = ReflectionUtil.GetAllPublicStaticFields(reservedPropertiesNestedType); Dictionary <string, PropertyInfo> reservedPropertyInfoByName = new Dictionary <string, PropertyInfo>(reservedAttributes.Length); result.ReservedPropertyInfoByReservedKey = reservedPropertyInfoByName; foreach (var reservedAttribue in reservedAttributes) { var property = extendableDtoType.GetProperty(reservedAttribue.Name, BindingFlags.Public | BindingFlags.Instance); Assumption.AssertNotNull(property, nameof(property)); string reservedKey = ReflectionUtil.GetStaticFieldValue <string>(reservedAttribue); Assumption.AssertNotNullOrWhiteSpace(reservedKey, nameof(reservedKey)); reservedPropertyInfoByName.Add(reservedKey, property); } return(result); }
static ExtendableDtoBase() { metadataByDerivedType = ExtendableDtoMetadata.BuildAll(); }