コード例 #1
0
        /// <summary>
        /// 输出对象_枚举条目.
        /// </summary>
        /// <param name="iw">带缩进输出者.</param>
        /// <param name="obj">object.</param>
        /// <param name="context">Context.</param>
        /// <param name="tp">类型.</param>
        public virtual void WriterObject_WriteEnumerate(IIndentedWriter iw, object obj, IndentedWriterContext context, Type tp)
        {
            if (0 != (m_Options & IndentedObjectFunctorOptions.NoEnumerate))
            {
                return;
            }
            IEnumerable lst = obj as IEnumerable;

            if (null == lst)
            {
                return;
            }
            int i = 0;

            foreach (object p in lst)
            {
                string name = string.Format("[{0}]", i);
                IndentedWriterObjectProc proc = null;
                if (null != context)
                {
                    proc = IndentedWriterUtil.LookupWriteProcAt(p, context, context.Procs);
                }
                if (null == proc && (m_WriterOptions & IndentedWriterMemberOptions.NoCommonProcs) == 0)
                {
                    if (IndentedObjectFunctor.CommonProc(null, p, context))
                    {
                        proc = IndentedObjectFunctor.CommonProc;
                    }
                }
                IndentedWriterUtil.WriteLineValue(iw, name, p, IndentedWriterValueOptions.Default, null);
                if (null != proc)
                {
                    proc(iw, p, context);
                }
                // next.
                ++i;
            }
        }
コード例 #2
0
        /// <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);
        }