Example #1
0
        /// <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);
        }
Example #2
0
        /// <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);
        }
Example #4
0
        /// <exclude />
        public DataTypeDescriptor BuildNewDataTypeDescriptor(Type typeToDescript)
        {
            if (typeToDescript == null)
            {
                throw new ArgumentNullException("typeToDescript");
            }

            return(ReflectionBasedDescriptorBuilder.Build(typeToDescript));
        }