/// <summary>
        /// Creates compilation syntax unit from the specified external type information and validation.
        /// </summary>
        /// <param name="info">The container external type information.</param>
        /// <param name="validation">The container type validation to use.</param>
        /// <param name="compilation">The project compilation.</param>
        /// <param name="generator">The syntax generator.</param>
        public static SyntaxNode CreateUnit(ICodeGenerateContainerInfo info, ICodeGenerateContainerValidation validation, Compilation compilation = null, SyntaxGenerator generator = null)
        {
            if (info == null)
            {
                throw new ArgumentNullException(nameof(info));
            }
            if (validation == null)
            {
                throw new ArgumentNullException(nameof(validation));
            }
            if (compilation == null)
            {
                compilation = CodeAnalysisEditorUtility.ProjectCompilation;
            }
            if (generator == null)
            {
                generator = CodeAnalysisEditorUtility.Generator;
            }

            if (!info.TryGetTargetType(out Type type))
            {
                throw new ArgumentException("The specified container external type info has invalid target type information.", nameof(info));
            }

            CodeGenerateContainer container = CreateContainer(info, validation, compilation);

            return(CodeGenerateContainerEditorUtility.CreateUnit(container, generator, type.Namespace));
        }
Example #2
0
        public void CreateUnit()
        {
            CodeGenerateContainerValidation validation = CodeGenerateContainerEditorUtility.DefaultValidation;
            CSharpCompilation compilation = CodeAnalysisEditorUtility.ProjectCompilation;
            SyntaxGenerator   generator   = CodeAnalysisEditorUtility.Generator;

            SyntaxNode unit = CodeGenerateContainerEditorUtility.CreateUnit(typeof(Target), validation, compilation, generator);

            string result   = unit.NormalizeWhitespace().ToFullString();
            string expected = File.ReadAllText(m_target3);

            Assert.AreEqual(expected, result);
        }
        /// <summary>
        /// Creates container from the specified external type information and validation.
        /// </summary>
        /// <param name="info">The container external type information.</param>
        /// <param name="validation">The container type validation to use.</param>
        /// <param name="compilation">The project compilation.</param>
        public static CodeGenerateContainer CreateContainer(ICodeGenerateContainerInfo info, ICodeGenerateContainerValidation validation, Compilation compilation = null)
        {
            if (info == null)
            {
                throw new ArgumentNullException(nameof(info));
            }
            if (validation == null)
            {
                throw new ArgumentNullException(nameof(validation));
            }
            if (compilation == null)
            {
                compilation = CodeAnalysisEditorUtility.ProjectCompilation;
            }

            if (!info.TryGetTargetType(out Type type))
            {
                throw new ArgumentException("The specified container external type info has invalid target type information.", nameof(info));
            }

            var container = new CodeGenerateContainer(type.Name, type.IsValueType);
            IEnumerable <FieldInfo>    fields     = validation.GetFields(type);
            IEnumerable <PropertyInfo> properties = validation.GetProperties(type);

            foreach (FieldInfo field in fields)
            {
                if (info.TryGetMember(field.Name, out CodeGenerateContainerInfo.MemberInfo member) && member.Active)
                {
                    if (CodeGenerateContainerEditorUtility.TryCreateField(compilation, field.Name, field.FieldType, false, out CodeGenerateContainerField containerField))
                    {
                        container.Fields.Add(containerField);
                    }
                }
            }

            foreach (PropertyInfo property in properties)
            {
                if (info.TryGetMember(property.Name, out CodeGenerateContainerInfo.MemberInfo member) && member.Active)
                {
                    if (CodeGenerateContainerEditorUtility.TryCreateField(compilation, property.Name, property.PropertyType, true, out CodeGenerateContainerField containerField))
                    {
                        container.Fields.Add(containerField);
                    }
                }
            }

            return(container);
        }
Example #4
0
        public void IsValidType()
        {
            bool result0 = CodeGenerateContainerEditorUtility.IsValidType(typeof(TargetClass));
            bool result1 = CodeGenerateContainerEditorUtility.IsValidType(typeof(TargetStruct));
            bool result2 = CodeGenerateContainerEditorUtility.IsValidType(typeof(TargetStaticClass));
            bool result3 = CodeGenerateContainerEditorUtility.IsValidType(typeof(Target));
            bool result4 = CodeGenerateContainerEditorUtility.IsValidType(typeof(TargetAbstractClass));
            bool result5 = CodeGenerateContainerEditorUtility.IsValidType(typeof(TargetGenericClass <>));
            bool result6 = CodeGenerateContainerEditorUtility.IsValidType(typeof(TestDelegate));

            Assert.True(result0);
            Assert.True(result1);
            Assert.False(result2);
            Assert.True(result3);
            Assert.False(result4);
            Assert.False(result5);
            Assert.False(result6);
        }