﻿using Microsoft.Cci;
using Microsoft.Cci.Extensions;

namespace Terrajobst.Cci
{
    public static class MemberExtensions
    {
        public static string GetNamespaceName(this ITypeDefinitionMember member)
        {
            return member.ContainingTypeDefinition.GetNamespaceName();
        }

        public static string GetTypeName(this ITypeDefinitionMember member)
        {
            return member.ContainingTypeDefinition.GetTypeName(false);
        }

        public static string GetMemberSignature(this ITypeDefinitionMember member)
        {
            if (member is IFieldDefinition)
                return member.Name.Value;

            return MemberHelper.GetMemberSignature(member, NameFormattingOptions.Signature |
                                                           NameFormattingOptions.TypeParameters |
                                                           NameFormattingOptions.ContractNullable |
                                                           NameFormattingOptions.OmitContainingType |
                                                           NameFormattingOptions.OmitContainingNamespace |
                                                           NameFormattingOptions.PreserveSpecialNames);
        }
    }
}

﻿using Microsoft.Cci;
using Microsoft.Cci.Extensions;

namespace Terrajobst.Cci
{
    public static class MemberExtensions
    {
        public static string GetNamespaceName(this IDefinition definition)
        {
            if (definition is ITypeDefinitionMember member)
                return member.ContainingTypeDefinition.GetNamespaceName();

            if (definition is ITypeDefinition type)
                return type.GetNamespace().GetNamespaceName();

            if (definition is INamespaceDefinition nsp)
                return nsp.FullName();

            return string.Empty;
        }

        public static string GetTypeName(this IDefinition definition)
        {
            if (definition is ITypeDefinitionMember member)
                return member.ContainingTypeDefinition.GetTypeName(false);

            if (definition is ITypeDefinition type)
                return type.GetTypeName(includeNamespace: false);

            return string.Empty;
        }

        public static string GetMemberSignature(this IDefinition definition)
        {
            if (definition is IFieldDefinition field)
                return field.Name.Value;

            if (definition is ITypeDefinitionMember member)
                return MemberHelper.GetMemberSignature(member, NameFormattingOptions.Signature |
                                                               NameFormattingOptions.TypeParameters |
                                                               NameFormattingOptions.ContractNullable |
                                                               NameFormattingOptions.OmitContainingType |
                                                               NameFormattingOptions.OmitContainingNamespace |
                                                               NameFormattingOptions.PreserveSpecialNames);

            return string.Empty;
        }
    }
}

Define extension method over IDefinition
Define extension method over IDefinition

We can currently only answer those questions for a type member, but not
for types and namespaces.
