Ejemplo n.º 1
0
        public string TryGetNameOfType(Type type, ITypeHelper typeHelper, NameOfTypeOptions options)
        {
            if (type is null)
            {
                throw new ArgumentNullException(nameof(type));
            }

            if (typeHelper is null)
            {
                throw new ArgumentNullException(nameof(typeHelper));
            }

            if (!type.IsGenericType)
            {
                return(null);
            }

            Type[]        arguments         = type.GetGenericArguments();
            List <string> argumentTypeNames = arguments.Select(t => typeHelper.NameOf(t.NotNull(), options)).ToList();

            var baseName = type.Name.Substring(0, type.Name.IndexOf('`'));

            var sb = new StringBuilder();

            if ((options & NameOfTypeOptions.IncludeNamespaces) != 0)
            {
                sb.Append(type.Namespace).Append('.');
            }

            sb.Append(baseName).Append('<');
            sb.Append(string.Join(",", argumentTypeNames));
            sb.Append('>');

            return(sb.ToString());
        }
Ejemplo n.º 2
0
        public string TryGetNameOfType(Type type, ITypeHelper typeHelper, NameOfTypeOptions options)
        {
            if (type is null)
            {
                throw new ArgumentNullException(nameof(type));
            }

            if (typeHelper is null)
            {
                throw new ArgumentNullException(nameof(typeHelper));
            }

            if ((options & NameOfTypeOptions.UseShorthandSyntax) == 0)
            {
                return(null);
            }

            if (!(type.IsGenericType))
            {
                return(null);
            }

            Type openGenericType = type.GetGenericTypeDefinition();

            if (openGenericType != typeof(Nullable <>))
            {
                return(null);
            }

            Type underlyingType = type.GetGenericArguments()[0].NotNull();

            return(typeHelper.NameOf(underlyingType) + "?");
        }
Ejemplo n.º 3
0
        public void NameOf_SmokeTests(Type type, NameOfTypeOptions options, string expected)
        {
            var container  = ContainerFactory.Bootstrap <ServicesBootstrapper>();
            var typeHelper = container.Resolve <ITypeHelper>();

            string output = typeHelper.TryGetNameOf(type, options);

            Assert.That(output, Is.EqualTo(expected));
        }
Ejemplo n.º 4
0
        string ITypeHelper.TryGetNameOf(Type type, NameOfTypeOptions options)
        {
            if (type is null)
            {
                throw new ArgumentNullException(nameof(type));
            }

            return(_NameOfRules.Select(r => r.TryGetNameOfType(type, this, options)).FirstOrDefault(n => !(n is null)));
        }
Ejemplo n.º 5
0
        public string TryGetNameOfType(Type type, ITypeHelper typeHelper, NameOfTypeOptions options)
        {
            if (typeHelper is null)
            {
                throw new ArgumentNullException(nameof(typeHelper));
            }

            if ((options & NameOfTypeOptions.UseCSharpKeywords) == 0)
            {
                return(null);
            }

            _CSharpKeywordTypeNames.TryGetValue(type, out string name);
            return(name);
        }
Ejemplo n.º 6
0
        public string TryGetNameOfType(Type type, ITypeHelper typeHelper, NameOfTypeOptions options)
        {
            if (type is null)
            {
                throw new ArgumentNullException(nameof(type));
            }

            if (typeHelper is null)
            {
                throw new ArgumentNullException(nameof(typeHelper));
            }

            if (type.IsGenericType)
            {
                return(null);
            }

            return((options & NameOfTypeOptions.IncludeNamespaces) != 0 ? type.FullName : type.Name);
        }
Ejemplo n.º 7
0
 public static string NameOf <T>(
     [NotNull] this ITypeHelper typeHelper, NameOfTypeOptions options = NameOfTypeOptions.Default)
 => NameOf(typeHelper, typeof(T), options);
Ejemplo n.º 8
0
 public static string NameOf(
     [NotNull] this ITypeHelper typeHelper, [NotNull] Type type,
     NameOfTypeOptions options = NameOfTypeOptions.Default)
 => typeHelper.TryGetNameOf(type, options) ?? type.FullName.NotNull();