示例#1
0
        /// <summary>
        /// Foramt it to a declaretion statement.
        /// </summary>
        /// <param name="ctor"></param>
        /// <returns></returns>
        public static string FormatToDeclaretion(this ConstructorInfo ctor)
        {
            var sb = ZString.CreateStringBuilder();

            MemberScope scope = ctor.GetScope();

            sb.Append(scope.Format());

            if (ctor.IsStatic)
            {
                sb.Append(" static");
            }
            else if (ctor.IsAbstract)
            {
                sb.Append(" abstract");
            }
            else if (ctor.IsVirtual)
            {
                sb.Append(" virtual");
            }

            sb.Append(" ");

            var name      = ctor.DeclaringType.Name;
            var delimiter = name.IndexOf('`');

            if (delimiter > 0)
            {
                sb.Append(name.Substring(0, delimiter));
            }
            else
            {
                sb.Append(name);
            }

            sb.Append("(");

            var parameters = ctor.GetParameters();

            if (parameters.Length > 0)
            {
                for (int i = 0; i < parameters.Length; ++i)
                {
                    sb.AppendLine();
                    sb.Append("\t");

                    if (i > 0)
                    {
                        sb.Append(", ");
                    }

                    sb.Append(parameters[i].ParameterType.FormatToReadableTypeName(false));

                    sb.Append(" ");
                    sb.Append(parameters[i].Name);
                }

                sb.AppendLine();
            }

            sb.Append(")");

            return(sb.ToString());
        }
示例#2
0
        /// <summary>
        /// Foramt it to a declaretion statement.
        /// </summary>
        /// <param name="property"></param>
        /// <returns></returns>
        public static string FormatToDeclaretion(this PropertyInfo property)
        {
            MethodInfo?getter = property.GetGetMethod(true);
            MethodInfo?setter = property.GetSetMethod(true);

            if (getter == null && setter == null)
            {
                return("");
            }

            MethodInfo method = getter ?? setter;

            MemberScope getterScope = getter?.GetScope() ?? MemberScope.None;
            MemberScope setterScope = setter?.GetScope() ?? MemberScope.None;
            MemberScope wideScope   = getterScope > setterScope ? getterScope : setterScope;

            var sb = ZString.CreateStringBuilder();

            if (!property.DeclaringType.IsInterface)
            {
                sb.Append(wideScope.Format());

                if (method.IsStatic)
                {
                    sb.Append(" static");
                }
                else if (method.IsAbstract)
                {
                    sb.Append(" abstract");
                }
                else if (method.IsVirtual)
                {
                    sb.Append(" virtual");
                }

                sb.Append(" ");
            }

            sb.Append(property.PropertyType.FormatToReadableTypeName(false));

            sb.Append(" ");
            sb.Append(property.Name);

            var parameters = property.GetIndexParameters();

            if (parameters.Length > 0)
            {
                sb.Append("[");

                for (int i = 0; i < parameters.Length; ++i)
                {
                    if (i > 0)
                    {
                        sb.Append(", ");
                    }

                    sb.Append(parameters[i].ParameterType.FormatToReadableTypeName(false));

                    sb.Append(" ");
                    sb.Append(parameters[i].Name);
                }

                sb.Append("]");
            }

            sb.Append(" {");

            if (getter != null)
            {
                sb.Append(" ");

                if (getterScope != wideScope)
                {
                    sb.Append(getterScope.Format());
                    sb.Append(" ");
                }

                sb.Append("get;");
            }

            if (setter != null)
            {
                sb.Append(" ");

                if (setterScope != wideScope)
                {
                    sb.Append(setterScope.Format());
                    sb.Append(" ");
                }

                sb.Append("set;");
            }

            sb.Append(" }");

            return(sb.ToString());
        }
        /// <summary>
        /// Foramt it to a declaretion statement.
        /// </summary>
        /// <param name="method"></param>
        /// <returns></returns>
        public static string FormatToDeclaretion(this MethodInfo method)
        {
            var genericArgs = method.IsGenericMethod ? method.GetGenericArguments() : Type.EmptyTypes;

            var sb = ZString.CreateStringBuilder();

            // modifier & scope

            MemberScope scope = method.GetScope();

            if (!method.DeclaringType.IsInterface)
            {
                sb.Append(scope.Format());

                if (method.IsStatic)
                {
                    sb.Append(" static");
                }
                else if (method.IsAbstract)
                {
                    sb.Append(" abstract");
                }
                else if (method.IsVirtual)
                {
                    sb.Append(" virtual");
                }

                sb.Append(" ");
            }

            // return value

            sb.Append(method.ReturnType.FormatToReadableTypeName(false));

            // name

            sb.Append(" ");
            sb.Append(method.Name);

            // generic arguments

            if (genericArgs.Length > 0)
            {
                sb.Append("<");

                for (int i = 0; i < genericArgs.Length; ++i)
                {
                    if (i > 0)
                    {
                        sb.Append(", ");
                    }
                    sb.Append(genericArgs[i].FormatToReadableTypeName(false));
                }

                sb.Append(">");
            }

            // arguments

            var parameters = method.GetParameters();

            if (parameters.Length > 0)
            {
                sb.AppendLine();
                sb.Append("(");

                for (int i = 0; i < parameters.Length; ++i)
                {
                    sb.AppendLine();
                    sb.Append("\t");

                    if (i > 0)
                    {
                        sb.Append(", ");
                    }

                    if (i == 0 && method.IsExtensionMethod())
                    {
                        sb.Append("this ");
                    }

                    if (parameters[i].IsIn)
                    {
                        sb.Append("in ");
                    }
                    if (parameters[i].IsOut)
                    {
                        sb.Append("out ");
                    }

                    sb.Append(parameters[i].ParameterType.FormatToReadableTypeName(false));

                    sb.Append(" ");
                    sb.Append(parameters[i].Name);

                    if (parameters[i].IsOptional)
                    {
                        if (parameters[i].DefaultValue == null)
                        {
                            sb.Append(" = null");
                        }
                        else if (parameters[i].ParameterType == typeof(string))
                        {
                            sb.AppendFormat(@" = ""{0}""", parameters[i].DefaultValue);
                        }
                        else if (parameters[i].ParameterType == typeof(char))
                        {
                            sb.AppendFormat(" = '{0}'", parameters[i].DefaultValue);
                        }
                        else
                        {
                            sb.AppendFormat(" = {0}", parameters[i].DefaultValue);
                        }
                    }
                }

                sb.AppendLine();
                sb.Append(")");
            }
            else
            {
                sb.Append("()");
            }


            // generic constraint

            foreach (var genericArg in genericArgs)
            {
                bool foundConstraint = false;

                foreach (var constraint in ClassDocUtility.EnumerateGenericConstraints(genericArg))
                {
                    if (foundConstraint)
                    {
                        sb.Append(", ");
                    }
                    else
                    {
                        sb.AppendLine();
                        sb.AppendFormat("where {0} : ", genericArg.Name);
                        foundConstraint = true;
                    }
                    sb.Append(constraint);
                }
            }

            return(sb.ToString());
        }