示例#1
0
        /// <summary>
        /// Gets the declaretion statement.
        /// </summary>
        /// <returns></returns>
        public string GetDelegateDeclaretion()
        {
            if (DelegateMethodInfo == null)
            {
                return("");
            }

            MethodInfo method = DelegateMethodInfo.Info;

            var genericArgs = Info.IsGenericType ? Info.GetGenericArguments() : Type.EmptyTypes;

            var sb = ZString.CreateStringBuilder();

            sb.Append("delegate ");

            // return value

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

            // name

            sb.Append(" ");
            sb.Append(Info.FormatToReadableTypeName(false));

            // 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 (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);
                }

                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());
        }
        /// <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());
        }