/// <summary> /// This method will return the data type descriptor for the given data type id. /// If the data type descriptor has not yet been created (file not existing) and /// the <paramref name="allowDataTypeCreation"/> is set to true, /// this method will try getting it through the <see cref="Composite.Data.DynamicTypes.Foundation.ReflectionBasedDescriptorBuilder"/> /// that will try locating the type from the data type id using refelction /// going through know assemblies. /// </summary> /// <param name="dataTypeId">The id of the data type.</param> /// <param name="allowDataTypeCreation"> /// If this is true and the data type descriptor does not exists, it will try to /// be created. /// </param> /// <returns></returns> public static DataTypeDescriptor GetDataTypeDescriptor(Guid dataTypeId, bool allowDataTypeCreation = false) { Initialize(); DataTypeDescriptor dataTypeDescriptor; _dataTypeDescriptorCache.TryGetValue(dataTypeId, out dataTypeDescriptor); if (dataTypeDescriptor != null) { return(dataTypeDescriptor); } if (!allowDataTypeCreation) { return(null); } foreach (Assembly assembly in AssemblyFacade.GetLoadedAssembliesFromBin()) { if (!AssemblyFacade.AssemblyPotentiallyUsesType(assembly, typeof(IData))) { // Ignoring assemblies that aren't referencing Composite.dll continue; } Type[] types; try { types = assembly.GetTypes(); } catch (ReflectionTypeLoadException ex) { throw new InvalidOperationException($"Failed to get types from assembly '{assembly.FullName}'", ex); } foreach (Type type in types) { if (type.GetInterfaces().Contains(typeof(IData))) { ImmutableTypeIdAttribute attribute = type.GetCustomAttributes(false).OfType <ImmutableTypeIdAttribute>().SingleOrDefault(); if (attribute == null || attribute.ImmutableTypeId != dataTypeId) { continue; } DataTypeDescriptor newDataTypeDescriptor = ReflectionBasedDescriptorBuilder.Build(type); PersistMetaData(newDataTypeDescriptor); return(newDataTypeDescriptor); } } } Log.LogError(LogTitle, $"No data type found with the given data type id '{dataTypeId}'"); return(null); }
/// <summary> /// This method will return the data type descriptor for the given data type id. /// If the data type descriptor has not yet been created (file not existing) and /// the <paramref name="allowTypeMetaDataCreation"/> is set to true, /// this method will try getting it through the <see cref="Composite.Data.DynamicTypes.Foundation.ReflectionBasedDescriptorBuilder"/> /// based on <paramref name="interfaceType"/>. /// </summary> /// <param name="interfaceType">The data type.</param> /// <param name="allowTypeMetaDataCreation"> /// If this is true and the data type descriptor does not exists, it will be created. /// </param> /// <returns></returns> public static DataTypeDescriptor GetDataTypeDescriptor(Type interfaceType, bool allowTypeMetaDataCreation = false) { Verify.ArgumentNotNull(interfaceType, nameof(interfaceType)); Initialize(); DataTypeDescriptor dataTypeDescriptor; Guid dataTypeId = interfaceType.GetImmutableTypeId(); _dataTypeDescriptorCache.TryGetValue(dataTypeId, out dataTypeDescriptor); if (dataTypeDescriptor != null) { return(dataTypeDescriptor); } if (!allowTypeMetaDataCreation) { return(null); } var newDataTypeDescriptor = ReflectionBasedDescriptorBuilder.Build(interfaceType); PersistMetaData(newDataTypeDescriptor); return(newDataTypeDescriptor); }
internal static void AddDataWrapperClassCode(CodeGenerationBuilder codeGenerationBuilder, Type interfaceType) { codeGenerationBuilder.AddReference(interfaceType.Assembly); DataTypeDescriptor dataTypeDescriptor = ReflectionBasedDescriptorBuilder.Build(interfaceType); AddDataWrapperClassCode(codeGenerationBuilder, dataTypeDescriptor); }
/// <exclude /> public DataTypeDescriptor BuildNewDataTypeDescriptor(Type typeToDescript) { if (typeToDescript == null) { throw new ArgumentNullException("typeToDescript"); } return(ReflectionBasedDescriptorBuilder.Build(typeToDescript)); }
/// <summary> /// Adds an interface the data type should inherit from /// </summary> /// <param name="interfaceType"></param> /// <param name="addInheritedFields"></param> internal void AddSuperInterface(Type interfaceType, bool addInheritedFields) { if (_superInterfaces.Contains(interfaceType) || interfaceType == typeof(IData)) { return; } _superInterfaces.Add(interfaceType); if (addInheritedFields) { foreach (PropertyInfo propertyInfo in interfaceType.GetProperties()) { if (propertyInfo.Name == nameof(IPageData.PageId) && interfaceType == typeof(IPageData)) { continue; } DataFieldDescriptor dataFieldDescriptor = ReflectionBasedDescriptorBuilder.BuildFieldDescriptor(propertyInfo, true); this.Fields.Add(dataFieldDescriptor); } } foreach (string propertyName in interfaceType.GetKeyPropertyNames()) { if (KeyPropertyNames.Contains(propertyName)) { continue; } PropertyInfo property = ReflectionBasedDescriptorBuilder.FindProperty(interfaceType, propertyName); if (DynamicTypeReflectionFacade.IsKeyField(property)) { this.KeyPropertyNames.Add(propertyName, false); } } foreach (var dataScopeIdentifier in DynamicTypeReflectionFacade.GetDataScopes(interfaceType)) { if (!this.DataScopes.Contains(dataScopeIdentifier)) { this.DataScopes.Add(dataScopeIdentifier); } } var superInterfaces = interfaceType.GetInterfaces().Where(t => typeof(IData).IsAssignableFrom(t)); foreach (Type superSuperInterfaceType in superInterfaces) { AddSuperInterface(superSuperInterfaceType, addInheritedFields); } }
/// <summary> /// Removes a super interface /// </summary> /// <param name="interfaceType">Type to remove</param> public void RemoveSuperInterface(Type interfaceType) { if (interfaceType == typeof(IData)) { return; } if (_superInterfaces.Contains(interfaceType)) { _superInterfaces.Remove(interfaceType); } foreach (PropertyInfo propertyInfo in interfaceType.GetProperties()) { var dataFieldDescriptor = ReflectionBasedDescriptorBuilder.BuildFieldDescriptor(propertyInfo, true); if (this.Fields.Contains(dataFieldDescriptor)) { this.Fields.Remove(dataFieldDescriptor); } if (DynamicTypeReflectionFacade.IsKeyField(propertyInfo) && this.KeyPropertyNames.Contains(propertyInfo.Name)) { this.KeyPropertyNames.Remove(propertyInfo.Name); } } foreach (var dataScopeIdentifier in DynamicTypeReflectionFacade.GetDataScopes(interfaceType)) { if (this.DataScopes.Contains(dataScopeIdentifier)) { this.DataScopes.Remove(dataScopeIdentifier); } } var superInterfaces = interfaceType.GetInterfaces().Where(t => typeof(IData).IsAssignableFrom(t)); foreach (Type superInterfaceType in superInterfaces) { RemoveSuperInterface(superInterfaceType); } }
/// <summary> /// Adds an interface the data type should inherit from /// </summary> /// <param name="interfaceType"></param> /// <param name="addInheritedFields"></param> internal void AddSuperInterface(Type interfaceType, bool addInheritedFields) { if (_superInterfaces.Contains(interfaceType) || interfaceType == typeof(IData)) { return; } _superInterfaces.Add(interfaceType); if (addInheritedFields) { foreach (PropertyInfo propertyInfo in interfaceType.GetProperties()) { if (propertyInfo.Name == "PageId" && interfaceType == typeof(IPageData)) { continue; } DataFieldDescriptor dataFieldDescriptor = ReflectionBasedDescriptorBuilder.BuildFieldDescriptor(propertyInfo, true); this.Fields.Add(dataFieldDescriptor); } } foreach (string propertyName in interfaceType.GetKeyPropertyNames()) { if (KeyPropertyNames.Contains(propertyName)) { continue; } PropertyInfo property = interfaceType.GetProperty(propertyName); if (property == null) { List <Type> superInterfaces = interfaceType.GetInterfacesRecursively(t => typeof(IData).IsAssignableFrom(t) && t != typeof(IData)); foreach (Type superInterface in superInterfaces) { property = superInterface.GetProperty(propertyName); if (property != null) { break; } } } Verify.IsNotNull(property, "Missing property '{0}' on type '{1}' or one of its interfaces".FormatWith(propertyName, interfaceType)); if (DynamicTypeReflectionFacade.IsKeyField(property)) { this.KeyPropertyNames.Add(propertyName, false); } } foreach (DataScopeIdentifier dataScopeIdentifier in DynamicTypeReflectionFacade.GetDataScopes(interfaceType)) { if (!this.DataScopes.Contains(dataScopeIdentifier)) { this.DataScopes.Add(dataScopeIdentifier); } } foreach (Type superSuperInterfaceType in interfaceType.GetInterfaces().Where(t => typeof(IData).IsAssignableFrom(t))) { AddSuperInterface(superSuperInterfaceType, addInheritedFields); } }