/// <summary>
        /// 输出简单方法的信息.
        /// </summary>
        /// <param name="iw">带缩进输出者.</param>
        /// <param name="mi">方法信息.</param>
        /// <param name="owner">所在对象.</param>
        /// <param name="name">方法名称. 为null表示默认.</param>
        /// <param name="options">选项.</param>
        /// <param name="appendcomment">追加注释. 可为 null 或 <c>String.Empty</c>. </param>
        /// <returns>若成功输出, 便返回true. 否则返回false.</returns>
        /// <remarks>
        /// 当 <paramref name="options"/> 具有 <see cref="IndentedWriterMemberOptions.AllowMethod"/> 标志时,还会显示方法信息. 方法必须有返回值, 且没有泛型参数, 其他情况有:
        /// <para>(暂不支持: 参数数量为0个);</para>
        /// <para>参数数量为1个, 且参数类型为枚举或bool.</para>
        /// </remarks>
        public static bool WriteSimpleMethod(IIndentedWriter iw, MethodInfo mi, object owner, string name, IndentedWriterValueOptions options, string appendcomment)
        {
            if (null == iw)
            {
                return(false);
            }
            if (null == mi)
            {
                return(false);
            }
            if (mi.IsSpecialName)
            {
                return(false);
            }
            if (null == mi.ReturnType)
            {
                return(false);
            }
            if (mi.ReturnType.Equals(typeof(void)))
            {
                return(false);
            }
            ParameterInfo[] pis = mi.GetParameters();
            if (false)
            {
            }
            else if (0 == pis.Length)
            {
                // 暂不支持.
                return(false);
            }
            else if (1 == pis.Length && !pis[0].IsOut)
            {
                Type argtype0 = pis[0].ParameterType;
#if (NETFX_CORE)
                TypeInfo argtype0info = argtype0.GetTypeInfo();
#endif
                IEnumerable lst        = null;          // 参数可用值列表.
                string      nameformat = null;          // 标题的格式.
                if (false)
                {
                }
#if (NETFX_CORE)
                else if (argtype0info.IsEnum)
                {
#else
                else if (argtype0.IsEnum)
                {
#endif
                    lst        = TypeUtil.GetEnumValues(argtype0);
                    nameformat = "{0:d}(0x{0:X}, {0})";
                }
                else if (argtype0.Equals(typeof(bool)))
                {
                    lst        = WriteSimpleMethod_List_bool;
                    nameformat = "{0}";
                }
                // show.
                if (null != lst)
                {
                    object[] args = new object[1];
                    if (null == name)
                    {
                        name = TypeUtil.GetMemberName(mi, DefaultMemberNameOption);
                    }
                    IndentedWriterUtil.WriteLineValue(iw, name, null, options | IndentedWriterValueOptions.AutoHideValue, appendcomment);
                    iw.Indent(null);
                    foreach (object p in lst)
                    {
                        string strname   = string.Format(nameformat, p);
                        string strappend = null;
                        object v         = null;
                        try {
                            args[0] = p;
                            v       = mi.Invoke(owner, args);
                        }
                        catch {
                        }
                        if (null == v)
                        {
                            strappend = string.Format("<{0}>", mi.ReturnType.Name);
                        }
                        IndentedWriterUtil.WriteLineValue(iw, strname, v, options, strappend);
                    }
                    iw.Unindent();
                    return(true);
                }
            }
            return(false);
        }
Exemple #2
0
 /// <summary>
 /// 取得类型名称.
 /// </summary>
 /// <param name="tp">type.</param>
 /// <returns>返回名称.</returns>
 /// <remarks>默认返回 <c>MemberInfoFormat.GetMemberName(tp, IndentedWriterUtil.DefaultTypeNameOption)</c>. </remarks>
 public static string GetTypeName(Type tp)
 {
     return(TypeUtil.GetMemberName(tp, IndentedWriterUtil.DefaultTypeNameOption));
 }