Esempio n. 1
0
        /// <summary>
        /// 根据指定的模块类型和方法创建命令描述对象
        /// </summary>
        /// <param name="moduleType">模块类型</param>
        /// <param name="method">方法对象</param>
        /// <param name="throwException">若操作失败是否抛出异常</param>
        /// <returns>命令描述对象</returns>
        public static WriterCommandDescriptor Create(Type moduleType, MethodInfo method, bool throwException)
        {
            if (moduleType == null)
            {
                if (throwException)
                {
                    throw new ArgumentNullException("moduleType");
                }
            }
            if (method == null)
            {
                if (throwException)
                {
                    throw new ArgumentNullException("method");
                }
                else
                {
                    return(null);
                }
            }
            WriterCommandDescriptionAttribute attr = (WriterCommandDescriptionAttribute)
                                                     Attribute.GetCustomAttribute(
                method,
                typeof(WriterCommandDescriptionAttribute),
                false);

            System.Reflection.ParameterInfo[] ps = method.GetParameters();
            if (attr == null ||
                method.ReturnType.Equals(typeof(void)) == false ||
                ps == null ||
                ps.Length != 2)
            {
                if (throwException)
                {
                    throw new ArgumentException(moduleType.FullName + "#" + method.Name);
                }
                else
                {
                    return(null);
                }
            }
            if (ps[0].ParameterType.Equals(typeof(object)) &&
                ps[1].ParameterType.Equals(typeof(WriterCommandEventArgs)))
            {
                string name = attr.Name;
                //System.EventHandler h = new EventHandler( objInstance , m.Name );// m.MethodHandle );
                if (name == null || name.Trim().Length == 0)
                {
                    name = method.Name;
                }
                WriterCommandDescriptor descriptor = new WriterCommandDescriptor();
                descriptor.CommandName   = name;
                descriptor.ContainerType = moduleType;
                descriptor.Method        = method;
                descriptor.ShortcutKey   = attr.ShortcutKey;
                descriptor.ImageResource = attr.ImageResource;
                descriptor.Description   = attr.Description;
                string resource = attr.ImageResource;
                if (resource == null || resource.Trim().Length == 0)
                {
                    resource = moduleType.Namespace + "." + name + ".bmp";
                }
                if (resource != null)
                {
                    descriptor.Image = CommandUtils.GetResourceImage(moduleType.Assembly, resource.Trim());
                }
                //if (descriptor.Image == null)
                //{
                //    descriptor.Image = CommandUtils.NullImage;
                //}
                if (descriptor.Description == null || descriptor.Description.Trim().Length == 0)
                {
                    // 获得说明
                    DescriptionAttribute da = (DescriptionAttribute)Attribute.GetCustomAttribute(
                        method,
                        typeof(DescriptionAttribute),
                        true);
                    if (da != null)
                    {
                        descriptor.Description = da.Description;
                    }
                }
                return(descriptor);
            }
            else
            {
                if (throwException)
                {
                    throw new ArgumentException(moduleType.FullName + "#" + method.Name);
                }
                else
                {
                    return(null);
                }
            }
        }
        /// <summary>
        /// 使用特定对象来初始化对象
        /// </summary>
        /// <param name="moduleType"></param>
        public static WriterCommandModuleDescriptor Create(Type moduleType, bool throwException)
        {
            if (moduleType == null)
            {
                if (throwException)
                {
                    throw new ArgumentNullException("moduleType");
                }
                else
                {
                    return(null);
                }
            }
            if (moduleType.IsSubclassOf(typeof(CSWriterCommandModule)) == false)
            {
                if (throwException)
                {
                    throw new ArgumentException(moduleType.FullName);
                }
                else
                {
                    return(null);
                }
            }
            WriterCommandModuleDescriptor descriptor = new WriterCommandModuleDescriptor();

            descriptor.ModuleType = moduleType;
            descriptor.Name       = moduleType.Name;
            WriterCommandDescriptionAttribute attr = (WriterCommandDescriptionAttribute)
                                                     Attribute.GetCustomAttribute(
                descriptor.ModuleType,
                typeof(WriterCommandDescriptionAttribute),
                false);

            if (attr != null)
            {
                descriptor.Name          = attr.Name;
                descriptor.Description   = attr.Description;
                descriptor.ImageResource = attr.ImageResource;
                string src = attr.ImageResource;
                if (src != null && src.Trim().Length > 0)
                {
                    descriptor.Image = CommandUtils.GetResourceImage(moduleType.Assembly, src.Trim());
                }
            }
            if (descriptor.Description == null || descriptor.Description.Trim().Length == 0)
            {
                // 获得说明
                DescriptionAttribute da = (DescriptionAttribute)Attribute.GetCustomAttribute(
                    descriptor.ModuleType,
                    typeof(DescriptionAttribute),
                    true);
                if (da != null)
                {
                    descriptor.Description = da.Description;
                }
            }


            // 分析成员方法,创建方法命令描述对象
            System.Reflection.MethodInfo[] ms = moduleType.GetMethods(
                System.Reflection.BindingFlags.Instance
                | System.Reflection.BindingFlags.Public
                | System.Reflection.BindingFlags.NonPublic);
            foreach (System.Reflection.MethodInfo m in ms)
            {
                //System.Console.WriteLine( m.DeclaringType.Name + "*" + m.Name);

                if (m.DeclaringType.Equals(typeof(System.Windows.Forms.Form)))
                {
                    continue;
                }
                if (m.DeclaringType.Equals(typeof(System.Windows.Forms.Control)))
                {
                    continue;
                }
                if (m.DeclaringType.Equals(typeof(object)))
                {
                    continue;
                }

                WriterCommandDescriptor cmd = WriterCommandDescriptor.Create(moduleType, m, false);
                if (cmd != null)
                {
                    descriptor.Commands.Add(cmd);
                }
            }//foreach
            descriptor.Commands.Sort();
            return(descriptor);
        }
Esempio n. 3
0
        /// <summary>
        /// 根据指定的命令对象类型创建命令描述对象
        /// </summary>
        /// <param name="commandType">命令类型</param>
        /// <param name="throwException">若操作失败是否抛出异常</param>
        /// <returns>创建的命令描述对象</returns>
        public static WriterCommandDescriptor Create(Type commandType, bool throwException)
        {
            if (commandType == null)
            {
                if (throwException)
                {
                    throw new ArgumentNullException("commandType");
                }
                else
                {
                    return(null);
                }
            }
            if (commandType.IsSubclassOf(typeof(WriterCommand)) == false)
            {
                if (throwException)
                {
                    throw new ArgumentException(commandType.FullName);
                }
                else
                {
                    return(null);
                }
            }
            WriterCommandDescriptionAttribute attr = (WriterCommandDescriptionAttribute)Attribute.GetCustomAttribute(
                commandType, typeof(WriterCommandDescriptionAttribute), false);
            //if (attr == null)
            //{
            //    if (throwException)
            //    {
            //        throw new ArgumentException(commandType.FullName);
            //    }
            //    else
            //    {
            //        return null;
            //    }
            //}
            WriterCommandDescriptor descriptor = new WriterCommandDescriptor();

            descriptor.ContainerType = commandType;
            descriptor.CommandName   = commandType.Name;
            if (attr != null)
            {
                descriptor.CommandName   = attr.Name;
                descriptor.Description   = attr.Description;
                descriptor.ImageResource = attr.ImageResource;
                descriptor.ShortcutKey   = attr.ShortcutKey;
                string src = attr.ImageResource;
                if (src != null && src.Trim().Length > 0)
                {
                    descriptor.Image = CommandUtils.GetResourceImage(commandType.Assembly, src.Trim());
                }
            }
            //if (descriptor.Image == null)
            //{
            //    descriptor.Image = CommandUtils.NullImage;
            //}
            if (descriptor.Description == null || descriptor.Description.Trim().Length == 0)
            {
                DescriptionAttribute da = (DescriptionAttribute)Attribute.GetCustomAttribute(
                    commandType,
                    typeof(DescriptionAttribute),
                    true);
                if (da != null)
                {
                    descriptor.Description = da.Description;
                }
            }
            return(descriptor);
        }