Example #1
0
        private void ApplyGenericTemplate(Type type, GenericModelTransferObject model)
        {
            IOptions modelOptions = this.options.Get(model);

            for (int index = 0; index < model.Template.Generics.Count; index++)
            {
                string alias = model.Template.Generics[index].Alias.Name;
                ModelTransferObject argument = this.Read(type.GenericTypeArguments[index], modelOptions);
                this.ApplyGenericTemplate(model, alias, argument);
            }
        }
Example #2
0
        public ModelTransferObject Read(Type type, IOptions caller = null)
        {
            ModelTransferObject model = new() { Language = ReflectionLanguage.Instance, Type = type };

            model.Name               = model.OriginalName = type.Name;
            model.Namespace          = type.Namespace;
            model.IsNullable         = !type.IsValueType;
            model.IsGeneric          = type.IsGenericType;
            model.IsGenericParameter = type.IsGenericParameter;
            model.FromSystem         = type.Namespace != null && type.Namespace.StartsWith("System");

            IOptions typeOptions = this.options.Get(type, caller);

            if (model.IsGeneric)
            {
                model.Name = model.OriginalName = type.Name.Split('`').First();
                model      = new GenericModelTransferObject(model);
            }
            ModelTransferObject existingModel = this.transferObjects.OfType <ModelTransferObject>().FirstOrDefault(entry => entry.Equals(model));

            if (existingModel != null)
            {
                // TODO: Replace complete if with cached model reading after cloning of TransferModels is fixed
                if (model.IsGeneric)
                {
                    GenericModelTransferObject genericModel = new(existingModel);
                    existingModel = genericModel;
                    this.options.Set(existingModel, typeOptions);
                    this.ApplyGenericTemplate(type, genericModel);
                    this.transferObjects.Add(existingModel);
                }
                return(existingModel);
            }
            // TODO: Uncomment cached model reading after cloning of TransferModels is fixed
            // existingModel = this.environment.TransferObjects.OfType<ModelTransferObject>().FirstOrDefault(entry => entry.Equals(model));
            // if (existingModel != null)
            // {
            //     return this.ReadExisting(existingModel, caller);
            // }
            this.options.Set(model, typeOptions);
            if (typeOptions.Ignore)
            {
                Logger.Trace($"{type.Name} ({type.Namespace}) ignored (decorated with {nameof(GenerateIgnoreAttribute)})");
                return(model);
            }
            if (type.IsGenericParameter)
            {
                return(model);
            }
            if (type.IsArray)
            {
                this.ReadArray(type, model);
            }
            else if (model.IsGeneric && model.FromSystem)
            {
                this.ReadGenericFromSystem(type, model);
            }
            else if (type.IsEnum)
            {
                this.transferObjects.Add(model);
                this.ReadEnum(type, model);
            }
            else if (!model.FromSystem)
            {
                this.transferObjects.Add(model);
                this.ReadClass(type, model, caller);
            }
            if (model.Name == nameof(Nullable))
            {
                model.IsNullable = true;
            }
            return(model);
        }