Beispiel #1
0
        /// <summary>
        /// Tries to create field from with the specified name and return type.
        /// <para>
        /// This method will create field if the type symbol for the specified return type can be got.
        /// </para>
        /// </summary>
        /// <param name="compilation">The project compilation.</param>
        /// <param name="name">The name of the field.</param>
        /// <param name="returnType">The return type of the field.</param>
        /// <param name="asAutoProperty">The value determines whether field will be create as auto property.</param>
        /// <param name="field">The created field.</param>
        public static bool TryCreateField(Compilation compilation, string name, Type returnType, bool asAutoProperty, out CodeGenerateContainerField field)
        {
            if (compilation == null)
            {
                throw new ArgumentNullException(nameof(compilation));
            }
            if (string.IsNullOrEmpty(name))
            {
                throw new ArgumentException("Value cannot be null or empty.", nameof(name));
            }
            if (returnType == null)
            {
                throw new ArgumentNullException(nameof(returnType));
            }

            if (compilation.TryConstructTypeSymbol(returnType, out ITypeSymbol typeSymbol))
            {
                string typeName = typeSymbol.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat);

                field = new CodeGenerateContainerField(name, typeName, null, asAutoProperty);
                return(true);
            }

            field = null;
            return(false);
        }
Beispiel #2
0
        /// <summary>
        /// Generates container syntax node using the specified syntax generator.
        /// </summary>
        /// <param name="generator">The syntax generator used to create container.</param>
        public SyntaxNode Generate(SyntaxGenerator generator)
        {
            if (generator == null)
            {
                throw new ArgumentNullException(nameof(generator));
            }

            SyntaxNode declaration = AsStruct
                ? generator.StructDeclaration(Name, null, Accessibility.Public)
                : generator.ClassDeclaration(Name, null, Accessibility.Public);

            for (int i = 0; i < Fields.Count; i++)
            {
                CodeGenerateContainerField field = Fields[i];

                declaration = generator.AddMembers(declaration, field.Generate(generator));
            }

            return(declaration);
        }