Ejemplo n.º 1
0
        /// <summary>
        /// 输出对象的各个成员.
        /// </summary>
        /// <param name="iw">带缩进输出者.</param>
        /// <param name="owner">欲查询成员的对象. 查询静态成员时可以设为 null.</param>
        /// <param name="tp">类型. 当 <paramref name="owner"/> 非 null 时, 可设为null, 即自动设为 <c>owner.GetType()</c> . </param>
        /// <param name="options">成员选项. 实际的成员选项是本参数与 <see cref="IndentedWriterContext.MemberOptions"/> 属性做或运算后的值. </param>
        /// <param name="handle">每个成员的处理过程, 可以为 null. 默认在调用时会将其 <c>sender</c>参数设为null. </param>
        /// <param name="context">环境对象, 可以为 null. 会嵌套传递. </param>
        /// <returns>是否成功.</returns>
        public static bool ForEachMember(IIndentedWriter iw, object owner, Type tp, IndentedWriterMemberOptions options, EventHandler <IndentedWriterMemberEventArgs> handle, IndentedWriterContext context)
        {
            bool rt = false;

            if (null == tp)
            {
                if (null == owner)
                {
                    return(rt);
                }
                tp = owner.GetType();
            }
            if (null == owner && (options & IndentedWriterMemberOptions.OnlyStatic) == 0)
            {
                throw new ArgumentException("options not has Static, but owner is null.", "bindingAttr");
            }
            // notify begin.
            if (null == context)
            {
                context = new IndentedWriterContext();                                  // 自动创建环境. 有可能以后调整设计, 所以后面还是检查 context.
            }
            if (null != context)
            {
                if (!context.NotifyForEachMemberBegin(iw, owner, tp, options, handle, context))
                {
                    return(false);
                }
            }
            // args
            IndentedWriterMemberEventArgs args = new IndentedWriterMemberEventArgs();

            args.IsCancel       = false;
            args.IsCancelAll    = false;
            args.HasDefault     = false;
            args.IndentedWriter = iw;
            args.Owner          = owner;
            args.OwnerType      = tp;
            args.MemberOptions  = options;
            //args.Procs = procs;
            args.Context = context;
            // foreach.
            IndentedWriterMemberOptions realoptions = options;

            if (null != context)
            {
                realoptions |= context.MemberOptions;
            }
            foreach (MemberInfo mi in GetMembers(tp, realoptions))
            {
                string AppendComment2 = null;                   // 备用的附加注释. 可能会在 args.AppendComment 为null时 使用.
                args.IsCancel      = true;
                args.HasDefault    = false;
                args.MemberInfo    = mi;
                args.MemberName    = TypeUtil.GetMemberNameAuto(mi, DefaultMemberNameOption);             //mi.Name;
                args.Value         = null;
                args.ValueOptions  = IndentedWriterValueOptions.ExistValue;
                args.AppendComment = null;
                args.WriteProc     = null;
                //Debug.WriteLine(mi.ToString());
                //
                //bool bOk = false;
                //object value = null;
                //IndentedWriterObjectProc writeproc = null;
                //IndentedWriterValueOptions iwvo = IndentedWriterValueOptions.ExistValue;
                //bool isdefault = false;
                // get value.
                if (false)
                {
                }
                else if (mi is FieldInfo)
                {
                    FieldInfo fi = mi as FieldInfo;
                    args.IsCancel = false;
                    if (true)
                    {
                        try {
                            args.Value        = fi.GetValue(owner);
                            args.ValueOptions = IndentedWriterValueOptions.Default;
                            args.HasDefault   = true;
                            AppendComment2    = string.Format("<{0}>", fi.FieldType.Name);
                        }
                        catch (Exception ex) {
                            Debug.WriteLine(ex);
                        }
                    }
                }
                else if (mi is PropertyInfo)
                {
                    PropertyInfo pi = mi as PropertyInfo;
                    args.IsCancel = false;
                    if (pi.CanRead && pi.GetIndexParameters().Length <= 0)
                    {
                        try {
                            args.Value        = pi.GetValue(owner, null);
                            args.ValueOptions = IndentedWriterValueOptions.Default;;
                            args.HasDefault   = true;
                            AppendComment2    = string.Format("<{0}>", pi.PropertyType.Name);
                        }
                        catch (Exception ex) {
                            Debug.WriteLine(ex);
                        }
                    }
                }
                else if (mi is MethodInfo)
                {
                    if ((realoptions & IndentedWriterMemberOptions.AllowMethod) != 0)
                    {
                        MethodInfo methodinfo = mi as MethodInfo;
                        args.IsCancel     = false;
                        args.ValueOptions = IndentedWriterValueOptions.AutoHideValue;
                        //args.MemberName = string.Format("{0}()", mi.Name);
                        AppendComment2 = string.Format("<{0}>", methodinfo.ReturnType.Name);
                    }
                }
                if (args.IsCancel)
                {
                    continue;
                }
                // get proc.
                if (null != args.Value)
                {
                    //args.WriteProc = LookupWriteProcAt(args.Value, context, procs);
                    if (null != context)
                    {
                        args.WriteProc = LookupWriteProcAt(args.Value, context, context.Procs);
                    }
                    //if (null == args.WriteProc && (realoptions & IndentedWriterMemberOptions.NoDefaultProcs) == 0) {
                    //    args.WriteProc = LookupWriteProc(args.Value, context);
                    //}
                    if (null == args.WriteProc && (realoptions & IndentedWriterMemberOptions.NoCommonProcs) == 0)
                    {
                        if (IndentedObjectFunctor.CommonProc(null, args.Value, context))
                        {
                            args.WriteProc = IndentedObjectFunctor.CommonProc;
                        }
                    }
                }
                // handle
                if (null != handle)
                {
                    //handle(context, mi, value, ref writeproc, ref iwvo, ref isdefault);
                    handle(null, args);
                }
                if (args.IsCancelAll)
                {
                    break;
                }
                if (args.IsCancel)
                {
                    continue;
                }
                // show.
                if (args.HasDefault)
                {
                    if (null == args.Value && null == args.AppendComment)
                    {
                        args.AppendComment = AppendComment2;
                    }
                    WriteLineValue(iw, args.MemberName, args.Value, args.ValueOptions, args.AppendComment);
                    if (null != args.WriteProc)
                    {
                        args.WriteProc(iw, args.Value, context);
                    }
                }
            }
            // notify begin.
            if (null != context)
            {
                context.NotifyForEachMemberEnd(iw, owner, tp, options, handle, context);
            }
            rt = true;
            return(rt);
        }
Ejemplo n.º 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);
        }
Ejemplo n.º 3
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));
 }