Ejemplo n.º 1
0
        /// <summary>
        /// 输出对象_输出成员.
        /// </summary>
        /// <param name="iw">带缩进输出者.</param>
        /// <param name="obj">object.</param>
        /// <param name="context">Context.</param>
        /// <param name="tp">类型.</param>
        /// <param name="showBaseName">是否显示基类类型的名称.</param>
        /// <param name="needtitle">是否需要输出标题.</param>
        public virtual void WriterObject_WriteMember(IIndentedWriter iw, object obj, IndentedWriterContext context, Type tp, bool showBaseName, ref bool needtitle)
        {
            bool needtitle2 = needtitle;

            try {
                IndentedWriterUtil.ForEachMember(iw, obj, tp, m_WriterOptions, delegate(object sender, IndentedWriterMemberEventArgs e) {
                    //Debug.WriteLine(string.Format("{0}: {1}", mi.Name, mi.MemberType));
                    if (needtitle2 && null != e && e.HasDefault)
                    {
                        // 仅当至少有一个成员, 才输出标题.
                        needtitle2 = false;
                        WriterObject_WriteTitle(iw, obj, context, tp, showBaseName);
                    }
                    // EnumerateNoSimple.
                    if (0 == (m_Options & IndentedObjectFunctorOptions.EnumerateNoSimple) && null != e.MemberInfo)
                    {
                        PropertyInfo pi = e.MemberInfo as PropertyInfo;
                        if (null != pi)
                        {
                            string name = pi.Name;
                            foreach (string s in EnumerateSimpleNames)
                            {
                                if (IndentedWriterUtil.StringComparer.Equals(s, name))
                                {
                                    e.IsCancel = true;
                                    break;
                                }
                            }
                        }
                    }
                    //
                    OnHandlerMember(this, e);
                }, context);
            }
            catch (Exception ex) {
                System.Diagnostics.Debug.WriteLine(ex);
            }
            needtitle = needtitle2;
        }
Ejemplo n.º 2
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;
            }
        }
Ejemplo n.º 3
0
 /// <summary>
 /// 输出类型的静态成员, 拥有选项参数.
 /// </summary>
 /// <param name="iw">带缩进输出者.</param>
 /// <param name="tp">type.</param>
 /// <param name="context">State Object. Can be null.</param>
 /// <param name="options">选项. 必须有 <see cref="IndentedWriterMemberOptions.OnlyStatic"/> 标志.</param>
 /// <returns>返回是否成功输出.</returns>
 /// <remarks>
 /// 当 <paramref name="options"/> 具有 <see cref="IndentedWriterMemberOptions.AllowMethod"/> 标志时,还会显示方法信息. 方法必须有返回值, 且没有泛型参数, 其他情况有:
 /// <para>参数数量为0个;</para>
 /// <para>参数数量为1个, 且参数类型为枚举或bool.</para>
 /// </remarks>
 public static bool WriteTypeStatic(IIndentedWriter iw, Type tp, IndentedWriterContext context, IndentedWriterMemberOptions options)
 {
     if (null == iw)
     {
         return(false);
     }
     if (null == tp)
     {
         return(false);
     }
     if (0 == (options & IndentedWriterMemberOptions.OnlyStatic))
     {
         return(false);
     }
     if (!iw.Indent(tp))
     {
         return(false);
     }
     iw.WriteLine(string.Format("# <{0}>", tp.FullName));
     //IndentedWriterUtil.ForEachMember(iw, null, tp, options, null, context);
     IndentedWriterUtil.ForEachMember(iw, null, tp, options, delegate(object sender, IndentedWriterMemberEventArgs e) {
         MethodInfo memberinfo = e.MemberInfo as MethodInfo;
         if (null != memberinfo)
         {
             //string name = memberinfo.Name;
             if (memberinfo.IsStatic &&
                 !memberinfo.IsSpecialName &&
                 null != memberinfo.ReturnType &&
                 !memberinfo.ReturnType.Equals(typeof(void)))
             {
                 ParameterInfo[] pis = memberinfo.GetParameters();
                 if (false)
                 {
                 }
                 else if (0 == pis.Length)
                 {
                     try {
                         e.Value = memberinfo.Invoke(null, null);
                         if (null == e.WriteProc)
                         {
                             e.WriteProc = IndentedObjectFunctor.CommonProc;
                         }
                         if (null == e.Value && null == e.AppendComment)
                         {
                             e.AppendComment = string.Format("<{0}>", memberinfo.ReturnType.Name);
                         }
                         e.HasDefault = true;
                     }
                     catch {
                         // 忽略.
                     }
                 }
                 else if (WriteSimpleMethod(iw, memberinfo, null, null, IndentedWriterValueOptions.Default, e.AppendComment))
                 {
                     e.IsCancel = true;
                 }
             }
         }
     }, context);
     iw.Unindent();
     return(true);
 }
Ejemplo n.º 4
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);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// 输出对象_检查对象.
        /// </summary>
        /// <param name="iw">带缩进输出者.</param>
        /// <param name="obj">object.</param>
        /// <param name="context">Context.</param>
        /// <param name="tp">返回 类型.</param>
        /// <param name="showBaseName">返回 是否显示基类类型的名称.</param>
        /// <returns>检查是否支持此对象的输出, 支持的话返回true, 否则返回false.</returns>
        public virtual bool WriterObject_CheckObject(IIndentedWriter iw, object obj, IndentedWriterContext context, out Type tp, out bool showBaseName)
        {
            // check.
            tp           = null;
            showBaseName = false;               // 是否显示基类类型的名称.
            if (null == obj)
            {
                return(false);
            }
            tp = obj.GetType();
            if (null == m_BaseType)
            {
                return(false);
            }
#if NETFX_CORE
            TypeInfo ti = tp.GetTypeInfo();
            if (ti.IsEnum)
            {
                return(false);
            }
            if (ti.IsPointer)
            {
                return(false);
            }
            if (ti.IsPrimitive)
            {
                return(false);
            }
#else
            if (tp.IsEnum)
            {
                return(false);
            }
            if (tp.IsPointer)
            {
                return(false);
            }
            if (tp.IsPrimitive)
            {
                return(false);
            }
#endif
            if ((m_Options & IndentedObjectFunctorOptions.OnlyType) != 0)
            {
                if (!m_BaseType.Equals(tp))
                {
                    return(false);
                }
            }
            else
            {
#if NETFX_CORE
                if (!m_BaseType.GetTypeInfo().IsAssignableFrom(ti))
                {
                    return(false);
                }
#else
                if (!m_BaseType.IsInstanceOfType(obj))
                {
                    return(false);
                }
#endif
                if ((m_Options & IndentedObjectFunctorOptions.OnlyMember) != 0)
                {
                    tp = m_BaseType;
                }
                else
                {
                    showBaseName = !m_BaseType.Equals(tp) && !m_BaseType.Equals(typeof(object));
                }
            }
            if ((m_Options & IndentedObjectFunctorOptions.AllowSimple) == 0)
            {
                if (IndentedWriterUtil.IsSimpleType(tp))
                {
                    return(false);
                }
            }
            return(true);
        }