public static SwitchDescriptorCollection CreateStaticSwitchDescriptors(ICustomAttributeProvider provider) { var descriptors = new SwitchDescriptorCollection(); var attrs = provider.GetCustomAttributes(typeof(CommandStaticSwitchAttribute), true); foreach (var item in attrs) { if (item is CommandStaticSwitchAttribute == false) continue; var attr = item as CommandStaticSwitchAttribute; var staticDescriptors = CommandDescriptor.GetSwitchDescriptors(attr.StaticType); descriptors.AddRange(Filter(staticDescriptors, attr.PropertyNames)); } return descriptors; }
private static IEnumerable<SwitchDescriptor> Filter(SwitchDescriptorCollection descriptors, params string[] propertyNames) { if (propertyNames.Any() == false) { foreach (var item in descriptors) { yield return item; } } else { foreach (var item in propertyNames) { yield return descriptors[item]; } } }
private static SwitchDescriptorCollection CreateSwitchDescriptors(Type type) { var descriptors = new SwitchDescriptorCollection(); var properties = type.GetProperties(); foreach (var item in properties) { if (item.GetCommandSwitchAttribute() == null) continue; if (item.CanWrite == false) throw new Exception(string.Format("'{0}' is not available because it cannot write.", item.Name)); descriptors.Add(new SwitchPropertyInfoDescriptor(item)); } foreach(var item in GetStaticSwitchDescriptors(type)) { descriptors.Add(item); } descriptors.Sort(); return descriptors; }