/// <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="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); }