/// <summary> /// Creates compilation unit from the specified container. /// </summary> /// <param name="container">The container used to generate compilation unit.</param> /// <param name="generator">The syntax generator.</param> /// <param name="namespaceRoot">The namespace of the container.</param> public static SyntaxNode CreateUnit(CodeGenerateContainer container, SyntaxGenerator generator = null, string namespaceRoot = null) { if (container == null) { throw new ArgumentNullException(nameof(container)); } if (generator == null) { generator = CodeAnalysisEditorUtility.Generator; } SyntaxNode unit = generator.CompilationUnit(); if (!string.IsNullOrEmpty(namespaceRoot)) { SyntaxNode namespaceDeclaration = generator.NamespaceDeclaration(namespaceRoot); namespaceDeclaration = generator.AddMembers(namespaceDeclaration, container.Generate(generator)); unit = generator.AddMembers(unit, namespaceDeclaration); } else { unit = generator.AddMembers(unit, container.Generate(generator)); } return(unit); }
/// <summary> /// Creates container from the specified type using specified validation. /// <para> /// The specified validation will not be used to validate specified type. /// </para> /// </summary> /// <param name="type">The target type.</param> /// <param name="validation">The type validation to use.</param> /// <param name="compilation">The project compilation.</param> public static CodeGenerateContainer Create(Type type, ICodeGenerateContainerValidation validation = null, Compilation compilation = null) { if (type == null) { throw new ArgumentNullException(nameof(type)); } if (validation == null) { validation = DefaultValidation; } if (compilation == null) { compilation = CodeAnalysisEditorUtility.ProjectCompilation; } var container = new CodeGenerateContainer(type.Name, type.IsValueType); foreach (FieldInfo field in validation.GetFields(type)) { if (validation.Validate(field)) { if (TryCreateField(compilation, field.Name, field.FieldType, false, out CodeGenerateContainerField containerField)) { container.Fields.Add(containerField); } } } foreach (PropertyInfo property in validation.GetProperties(type)) { if (validation.Validate(property)) { if (TryCreateField(compilation, property.Name, property.PropertyType, true, out CodeGenerateContainerField containerField)) { container.Fields.Add(containerField); } } } return(container); }
/// <summary> /// Creates compilation unit from the specified container type using validation. /// </summary> /// <param name="type">The type of the container.</param> /// <param name="validation">The validation to use.</param> /// <param name="compilation">The project compilation.</param> /// <param name="generator">The syntax generator.</param> public static SyntaxNode CreateUnit(Type type, ICodeGenerateContainerValidation validation = null, Compilation compilation = null, SyntaxGenerator generator = null) { if (type == null) { throw new ArgumentNullException(nameof(type)); } if (validation == null) { validation = DefaultValidation; } if (compilation == null) { compilation = CodeAnalysisEditorUtility.ProjectCompilation; } if (generator == null) { generator = CodeAnalysisEditorUtility.Generator; } CodeGenerateContainer container = Create(type, validation, compilation); return(CreateUnit(container, generator, type.Namespace)); }