protected WriterCommandList CreateCommands() { WriterCommandModuleDescriptor md = WriterCommandModuleDescriptor.Create( this.GetType(), false); WriterCommandList list = new WriterCommandList(); foreach (WriterCommandDescriptor d in md.Commands) { WriterCommandDelegate cmd = new WriterCommandDelegate(); cmd.Name = d.CommandName; cmd.ShortcutKey = d.ShortcutKey; cmd.ToolbarImage = d.Image; cmd.Description = d.Description; cmd.Handler = (WriterCommandEventHandler)Delegate.CreateDelegate( typeof(WriterCommandEventHandler), this, d.Method); list.Add(cmd); } return(list); }
/// <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); }