Ejemplo n.º 1
0
        internal static void WriteFriendlyName(
            this TranslationBuffer buffer,
            Type type,
            TranslationSettings settings)
        {
            if (type.FullName == null)
            {
                // An open generic parameter Type:
                return;
            }

            if (type.IsArray)
            {
                buffer.WriteFriendlyName(type.GetElementType(), settings);
                buffer.WriteToTranslation("[]");
                return;
            }

            if (!type.IsGenericType())
            {
                var substitutedTypeName = type.GetSubstitutionOrNull();

                if (type.IsNested)
                {
                    buffer.WriteFriendlyName(type.DeclaringType, settings);
                    buffer.WriteToTranslation('.');
                    buffer.WriteToTranslation(substitutedTypeName ?? type.Name);
                    return;
                }

                if (substitutedTypeName != null)
                {
                    buffer.WriteToTranslation(substitutedTypeName);
                    return;
                }

                buffer.WriteTypeNamespaceIfRequired(type, settings);
                buffer.WriteToTranslation(type.Name);
                return;
            }

            Type underlyingNullableType;

            if ((underlyingNullableType = Nullable.GetUnderlyingType(type)) == null)
            {
                buffer.WriteGenericTypeName(type, settings);
                return;
            }

            buffer.WriteFriendlyName(underlyingNullableType, settings);
            buffer.WriteToTranslation('?');
        }
        internal static string GetFriendlyName(this Type type, TranslationSettings translationSettings)
        {
            var buffer = new TranslationBuffer((type.FullName ?? type.ToString()).Length);

            buffer.WriteFriendlyName(type, translationSettings);

            return(buffer.GetContent());
        }
        /// <summary>
        /// Translates the given <paramref name="method"/> into a readable string.
        /// </summary>
        /// <param name="method">The MethodInfo to translate.</param>
        /// <returns>A readable string version of the given <paramref name="method"/>.</returns>
        public static string Translate(MethodInfo method)
        {
            if (method == null)
            {
                return("[Method not found]");
            }

            var buffer = new TranslationBuffer(method.ToString().Length);

            WriteModifiersToTranslation(method, buffer);

            var isProperty = method.IsPropertyGetterOrSetterCall(out var property);

            buffer.WriteFriendlyName(isProperty ? property.PropertyType : method.ReturnType);
            buffer.WriteSpaceToTranslation();

            if (method.DeclaringType != null)
            {
                buffer.WriteFriendlyName(method.DeclaringType);
                buffer.WriteToTranslation('.');
            }

            if (isProperty)
            {
                buffer.WriteToTranslation(property.Name);
                buffer.WriteToTranslation((method.ReturnType != typeof(void)) ? " { get; }" : " { set; }");

                return(buffer.GetContent());
            }

            buffer.WriteToTranslation(method.Name);

            if (method.IsGenericMethod)
            {
                WriteGenericArgumentsToTranslation(method.GetGenericArguments(), buffer);
            }

            WriteParametersToTranslation(method, buffer);

            return(buffer.GetContent());
        }
Ejemplo n.º 4
0
        internal static string GetFriendlyName(this Type type, TranslationSettings translationSettings)
        {
            if (type.FullName == null)
            {
                // An open generic parameter Type:
                return(null);
            }

            var buffer = new TranslationBuffer(type.FullName.Length);

            buffer.WriteFriendlyName(type, translationSettings);

            return(buffer.GetContent());
        }
        /// <summary>
        /// Translates the given <paramref name="type"/> into a readable string.
        /// </summary>
        /// <param name="type">The Type to translate.</param>
        /// <returns>A readable string version of the given <paramref name="type"/>.</returns>
        public static string Translate(Type type)
        {
            if (type == null)
            {
                return("[Type not found]");
            }

            var buffer = new TranslationBuffer(type.ToString().Length);

            WriteModifiersToTranslation(type, buffer);

            buffer.WriteFriendlyName(type);

            return(buffer.GetContent());
        }
Ejemplo n.º 6
0
        private static void WriteGenericTypeName(
            this TranslationBuffer buffer,
            Type type,
            int numberOfParameters,
            IList <Type> typeArguments,
            TranslationSettings settings)
        {
            var isAnonType =
                type.Name.StartsWith('<') &&
                (type.Name.IndexOf("AnonymousType", StringComparison.Ordinal)) != -1;

            if (isAnonType && (settings.AnonymousTypeNameFactory != null))
            {
                buffer.WriteToTranslation(settings.AnonymousTypeNameFactory.Invoke(type));
                return;
            }

            string typeName;

            if (isAnonType)
            {
                typeName = "AnonymousType";
            }
            else
            {
                var parameterCountIndex = type.Name.IndexOf("`" + numberOfParameters, StringComparison.Ordinal);
                typeName = type.Name.Substring(0, parameterCountIndex);
            }

            buffer.WriteToTranslation(typeName);
            buffer.WriteToTranslation('<');

            for (var i = 0; ;)
            {
                var typeArgument = typeArguments[i++];

                buffer.WriteFriendlyName(typeArgument, settings);

                if (i == typeArguments.Count)
                {
                    break;
                }

                buffer.WriteToTranslation(", ");
            }

            buffer.WriteToTranslation('>');
        }
        private static void WriteParametersToTranslation(MethodBase method, TranslationBuffer buffer)
        {
            var parameters = method.GetParameters();

            if (!parameters.Any())
            {
                buffer.WriteToTranslation("()");
                return;
            }

            buffer.WriteNewLineToTranslation();
            buffer.WriteToTranslation('(');
            buffer.Indent();

            for (var i = 0; ;)
            {
                var parameter     = parameters[i];
                var parameterType = parameter.ParameterType;

                buffer.WriteNewLineToTranslation();

                if (parameter.IsOut)
                {
                    buffer.WriteToTranslation("out ");
                    parameterType = parameterType.GetElementType();
                }
                else if (parameterType.IsByRef)
                {
                    buffer.WriteToTranslation("ref ");
                    parameterType = parameterType.GetElementType();
                }

                buffer.WriteFriendlyName(parameterType);
                buffer.WriteSpaceToTranslation();
                buffer.WriteToTranslation(parameter.Name);

                if (++i == parameters.Length)
                {
                    break;
                }

                buffer.WriteToTranslation(',');
            }

            buffer.Unindent();
            buffer.WriteNewLineToTranslation();
            buffer.WriteToTranslation(')');
        }
        /// <summary>
        /// Translates the given <paramref name="ctor"/> into a readable string.
        /// </summary>
        /// <param name="ctor">The ConstructorInfo to translate.</param>
        /// <returns>A readable string version of the given <paramref name="ctor"/>.</returns>
        public static string Translate(ConstructorInfo ctor)
        {
            if (ctor == null)
            {
                return("[Constructor not found]");
            }

            var buffer = new TranslationBuffer(ctor.ToString().Length);

            WriteAccessibilityToTranslation(ctor, buffer);

            buffer.WriteFriendlyName(ctor.DeclaringType);

            WriteParametersToTranslation(ctor, buffer);

            return(buffer.GetContent());
        }
        private static void WriteGenericArgumentsToTranslation(IList <Type> genericArguments, TranslationBuffer buffer)
        {
            var genericArgumentTypes = genericArguments;

            buffer.WriteToTranslation('<');

            for (var i = 0; ;)
            {
                var argumentType = genericArgumentTypes[i];

                buffer.WriteFriendlyName(argumentType);

                if (++i == genericArgumentTypes.Count)
                {
                    break;
                }

                buffer.WriteToTranslation(", ");
            }

            buffer.WriteToTranslation('>');
        }