private static IEnumerable <ITypeReference> GetBaseTypes(this ITypeReference typeRef)
        {
            ITypeDefinition type = typeRef.GetDefinitionOrNull();

            if (type == null)
            {
                yield break;
            }

            foreach (var baseTypeRef in type.BaseClasses)
            {
                yield return(baseTypeRef);

                foreach (var nestedBaseTypeRef in GetBaseTypes(baseTypeRef))
                {
                    yield return(nestedBaseTypeRef);
                }
            }
        }
예제 #2
0
        private void WriteTypeName(ITypeReference type, bool noSpace = false, IEnumerable <ICustomAttribute> attributes = null, bool useTypeKeywords = true,
                                   bool omitGenericTypeList          = false)
        {
            if (attributes != null && IsDynamic(attributes))
            {
                WriteKeyword("dynamic", noSpace: noSpace);
                return;
            }

            NameFormattingOptions namingOptions = NameFormattingOptions.TypeParameters | NameFormattingOptions.ContractNullable;

            if (useTypeKeywords)
            {
                namingOptions |= NameFormattingOptions.UseTypeKeywords;
            }

            if (_forCompilationIncludeGlobalprefix)
            {
                namingOptions |= NameFormattingOptions.UseGlobalPrefix;
            }

            if (!_forCompilation)
            {
                namingOptions |= NameFormattingOptions.OmitContainingNamespace;
            }

            if (omitGenericTypeList)
            {
                namingOptions |= NameFormattingOptions.EmptyTypeParameterList;
            }

            void WriteTypeNameInner(ITypeReference typeReference)
            {
                string name = TypeHelper.GetTypeName(typeReference, namingOptions);

                if (CSharpCciExtensions.IsKeyword(name))
                {
                    _writer.WriteKeyword(name);
                }
                else
                {
                    _writer.WriteTypeName(name);
                }
            }

            var definition = type.GetDefinitionOrNull();

            if (definition is IGenericTypeInstance genericType && genericType.IsValueTuple())
            {
                string[] names = attributes.GetValueTupleNames();

                _writer.WriteSymbol("(");

                int i = 0;
                foreach (var parameter in genericType.GenericArguments)
                {
                    if (i != 0)
                    {
                        _writer.WriteSymbol(",");
                        _writer.WriteSpace();
                    }

                    WriteTypeNameInner(parameter);

                    if (names?[i] != null)
                    {
                        _writer.WriteSpace();
                        _writer.WriteIdentifier(names[i]);
                    }

                    i++;
                }

                _writer.WriteSymbol(")");
            }