internal static bool HasRequiredAttribute(MemberInfo member) { return(MetadataHelper.GetAttribute <RequiredAttribute>(member) != null); }
public static IEnumerable <EnumMemberInfo> GetEnumSource(Type enumType, bool useUnderlyingEnumValue = true, IValueConverter nameConverter = null, bool splitNames = false, EnumMembersSortMode sortMode = EnumMembersSortMode.Default, Func <string, bool, string> getKnownImageUriCallback = null, bool showImage = true, bool showName = true, Func <string, ImageSource> getSvgImageSource = null) { if (enumType == null || !enumType.IsEnum) { return(Enumerable.Empty <EnumMemberInfo>()); } Func <string, ImageSource> getImageSource = uri => (ImageSource) new ImageSourceConverter().ConvertFrom(uri); var result = enumType.GetFields(BindingFlags.Static | BindingFlags.Public) .Where(field => DataAnnotationsAttributeHelper.GetAutoGenerateField(field)) .Select(field => { Enum value = (Enum)field.GetValue(null); string name = GetEnumName(field, value, nameConverter, splitNames); var imageInfo = GetImageInfo(MetadataHelper.GetAttribute <ImageAttribute>(field), MetadataHelper.GetAttribute <DXImageAttribute>(field), null, getKnownImageUriCallback); string imageUri = ViewModelBase.IsInDesignMode ? null : (imageInfo.Item1 ?? imageInfo.Item2); Func <ImageSource> getImage = () => imageUri.With(CanCreateSvgImageSource(getSvgImageSource, imageUri) ? getSvgImageSource : getImageSource); return(new EnumMemberInfo(name, DataAnnotationsAttributeHelper.GetFieldDescription(field), useUnderlyingEnumValue ? GetUnderlyingEnumValue(value) : value, showImage, showName, getImage, DataAnnotationsAttributeHelper.GetFieldOrder(field))); }); switch (sortMode) { case EnumMembersSortMode.DisplayName: result = result.OrderBy(x => x.Name); break; case EnumMembersSortMode.DisplayNameDescending: result = result.OrderByDescending(x => x.Name); break; case EnumMembersSortMode.DisplayNameLength: result = result.OrderBy(x => x.Name.Length); break; case EnumMembersSortMode.DisplayNameLengthDescending: result = result.OrderByDescending(x => x.Name.Length); break; } return(result.OrderBy(x => (x.Order != null) ? x.Order : DefaultDisplayOrder).ToArray()); }
static DisplayAttribute GetFieldDisplayAttribute(FieldInfo field) { return(MetadataHelper.GetAttribute <DisplayAttribute>(field)); }