diff --git a/Util.sln b/Util.sln index 2f6caefd..381709d9 100644 --- a/Util.sln +++ b/Util.sln @@ -1,6 +1,6 @@ Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio 15 -VisualStudioVersion = 15.0.27130.2010 +VisualStudioVersion = 15.0.27130.2027 MinimumVisualStudioVersion = 10.0.40219.1 Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Util.Samples.Webs", "sample\Util.Samples.Webs\Util.Samples.Webs.csproj", "{323B64B7-5CE0-4222-B9ED-9F418730897C}" EndProject diff --git a/sample/Util.Samples.Webs/Services/Dtos/Customers/CustomersDto.cs b/sample/Util.Samples.Webs/Services/Dtos/Customers/CustomersDto.cs index 563b34cf..814e5011 100644 --- a/sample/Util.Samples.Webs/Services/Dtos/Customers/CustomersDto.cs +++ b/sample/Util.Samples.Webs/Services/Dtos/Customers/CustomersDto.cs @@ -33,6 +33,7 @@ namespace Donau.Services.Dtos.Customers { /// 余额 /// [DataMember] + [StringLength( 6, ErrorMessage = "昵称输入过长,不能超过30位" )] [Display( Name = "余额" )] public double Balance { get; set; } diff --git a/sample/Util.Samples.Webs/Views/Home/Demo.cshtml b/sample/Util.Samples.Webs/Views/Home/Demo.cshtml index 61b42e61..3ad96ccc 100644 --- a/sample/Util.Samples.Webs/Views/Home/Demo.cshtml +++ b/sample/Util.Samples.Webs/Views/Home/Demo.cshtml @@ -25,8 +25,9 @@ - @x.TextBox( t => t.Name ) - @x.Select( t => t.Nation ) + + + @x.Radio( t => t.Gender ) diff --git a/src/Util.Ui.Angular/Material/Commons/Internal/Helper.cs b/src/Util.Ui.Angular/Material/Commons/Internal/Helper.cs index 1c2ef542..4b4a8187 100644 --- a/src/Util.Ui.Angular/Material/Commons/Internal/Helper.cs +++ b/src/Util.Ui.Angular/Material/Commons/Internal/Helper.cs @@ -1,10 +1,13 @@ using System; using System.ComponentModel.DataAnnotations; +using System.Linq; using System.Linq.Expressions; using System.Reflection; +using Microsoft.AspNetCore.Mvc.ViewFeatures; using Util.Helpers; using Util.Ui.Attributes; using Util.Ui.Components; +using Util.Ui.Configs; using Util.Ui.Extensions; using Util.Ui.Operations.Forms; using Util.Ui.Operations.Forms.Validations; @@ -22,10 +25,10 @@ namespace Util.Ui.Material.Commons.Internal { /// 表单控件 /// 属性表达式 /// 成员 - public static void InitModelControl( IFormControl control, Expression> expression, MemberInfo member ) { + public static void InitControl( IFormControl control, Expression> expression, MemberInfo member ) { control.Name( Util.Helpers.String.FirstLowerCase( Lambda.GetName( expression ) ) ); control.Placeholder( Reflection.GetDisplayNameOrDescription( member ) ); - InitModel( control,expression ); + InitModel( control, expression ); InitRequired( control, expression ); } @@ -40,24 +43,22 @@ namespace Util.Ui.Material.Commons.Internal { } /// - /// 初始化必填项验证 + /// 获取模型绑定 /// - public static void InitRequired( IRequired control, Expression> expression ) { - var attribute = Lambda.GetAttribute( expression ); - if ( attribute == null ) - return; - control.Required( attribute.ErrorMessage ); + /// 模型类型 + /// 属性类型 + /// 属性表达式 + public static string GetModel( Expression> expression ) { + return GetModel( GetModelName(), GetPropertyName( expression ) ); } /// /// 获取模型绑定 /// - /// 模型类型 - /// 属性类型 - public static string GetModel( Expression> expression ) { - var modelName = Util.Helpers.String.FirstLowerCase( GetModelName() ); - var propertyName = Util.Helpers.String.FirstLowerCase( GetPropertyName( expression ) ); - if ( string.IsNullOrWhiteSpace( modelName ) || string.IsNullOrWhiteSpace( propertyName ) ) + private static string GetModel( string modelName, string propertyName ) { + modelName = Util.Helpers.String.FirstLowerCase( modelName ); + propertyName = Util.Helpers.String.FirstLowerCase( propertyName ); + if( string.IsNullOrWhiteSpace( modelName ) || string.IsNullOrWhiteSpace( propertyName ) ) return string.Empty; return $"{modelName}&&{modelName}.{propertyName}"; } @@ -67,10 +68,16 @@ namespace Util.Ui.Material.Commons.Internal { /// private static string GetModelName() { Type type = Common.GetType(); - if ( type.GetCustomAttribute() is ModelAttribute attribute ) { + return GetModelName( type ); + } + + /// + /// 获取模型名称 + /// + private static string GetModelName( Type modelType ) { + if( modelType.GetCustomAttribute() is ModelAttribute attribute ) return attribute.Ignore ? string.Empty : attribute.Model; - } - return typeof( TModel ).Name; + return modelType.Name; } /// @@ -78,7 +85,85 @@ namespace Util.Ui.Material.Commons.Internal { /// private static string GetPropertyName( Expression> expression ) { var attribute = Lambda.GetAttribute( expression ); - return attribute == null ? Lambda.GetName( expression ) : attribute.Ignore ? string.Empty : attribute.Model; ; + return GetPropertyName( attribute, Lambda.GetName( expression ) ); + } + + /// + /// 获取属性名称 + /// + private static string GetPropertyName( ModelAttribute attribute,string propertyName ) { + return attribute == null ? propertyName : attribute.Ignore ? string.Empty : attribute.Model; ; + } + + /// + /// 初始化必填项验证 + /// + public static void InitRequired( IRequired control, Expression> expression ) { + var attribute = Lambda.GetAttribute( expression ); + if( attribute == null ) + return; + control.Required( attribute.ErrorMessage ); + } + + /// + /// 初始化模型配置 + /// + /// 配置 + /// 属性表达式 + /// 成员 + public static void InitConfig( IConfig config, ModelExpression expression, MemberInfo member ) { + config.SetAttribute( UiConst.Name, Util.Helpers.String.FirstLowerCase( expression.Name ) ); + config.SetAttribute( UiConst.Placeholder, Reflection.GetDisplayNameOrDescription( member ) ); + InitModel( config, expression ); + InitRequired( config, expression ); + } + + /// + /// 初始化模型 + /// + public static void InitModel( IConfig config, ModelExpression expression ) { + var model = GetModel( expression ); + if( string.IsNullOrWhiteSpace( model ) ) + return; + config.SetAttribute( UiConst.Model, model ); + } + + /// + /// 获取模型绑定 + /// + public static string GetModel( ModelExpression expression ) { + return GetModel( GetModelName( expression ), GetPropertyName( expression ) ); + } + + /// + /// 获取模型名称 + /// + private static string GetModelName( ModelExpression expression ) { + Type type = expression.Metadata.ContainerType; + return GetModelName( type ); + } + + /// + /// 获取属性名称 + /// + private static string GetPropertyName( ModelExpression expression ) { + var attribute = expression.Metadata.ModelType.GetCustomAttribute(); + return GetPropertyName( attribute, expression.Name ); + } + + /// + /// 初始化必填项验证 + /// + /// 配置 + /// 属性表达式 + public static void InitRequired( IConfig config, ModelExpression expression ) { + if ( expression.Metadata.IsRequired == false ) + return; + var attribute = expression.GetValidationAttribute(); + if( attribute == null ) + return; + config.SetAttribute( UiConst.Required, true ); + config.SetAttribute( UiConst.RequiredMessage, attribute.ErrorMessage ); } } } diff --git a/src/Util.Ui.Angular/Material/Extensions/Extensions.Select.cs b/src/Util.Ui.Angular/Material/Extensions/Extensions.Select.cs index aba859e7..8a42b860 100644 --- a/src/Util.Ui.Angular/Material/Extensions/Extensions.Select.cs +++ b/src/Util.Ui.Angular/Material/Extensions/Extensions.Select.cs @@ -61,7 +61,7 @@ namespace Util.Ui.Material.Extensions { /// 组件类型 /// 组件实例 public static TComponent Bool( this TComponent component ) where TComponent : IItem { - return component.Add( "是", "true" ).Add( "否", "false" ); + return component.Add( "是", true ).Add( "否", false ); } } } diff --git a/src/Util.Ui.Angular/Material/Forms/Configs/SelectConfig.cs b/src/Util.Ui.Angular/Material/Forms/Configs/SelectConfig.cs index 833938e8..a8d4096c 100644 --- a/src/Util.Ui.Angular/Material/Forms/Configs/SelectConfig.cs +++ b/src/Util.Ui.Angular/Material/Forms/Configs/SelectConfig.cs @@ -29,8 +29,9 @@ namespace Util.Ui.Material.Forms.Configs { /// 添加项集合 /// /// 列表项集合 - public void AddItems( params Item[] items ) { + public SelectConfig AddItems( params Item[] items ) { Items.AddRange( items ); + return this; } /// diff --git a/src/Util.Ui.Angular/Material/Forms/Models/ModelSelect.cs b/src/Util.Ui.Angular/Material/Forms/Models/ModelSelect.cs index d422afac..54be9c74 100644 --- a/src/Util.Ui.Angular/Material/Forms/Models/ModelSelect.cs +++ b/src/Util.Ui.Angular/Material/Forms/Models/ModelSelect.cs @@ -37,7 +37,7 @@ namespace Util.Ui.Material.Forms.Models { /// 初始化 /// private void Init() { - Helper.InitModelControl( this, _expression, _memberInfo ); + Helper.InitControl( this, _expression, _memberInfo ); InitType(); } diff --git a/src/Util.Ui.Angular/Material/Forms/Models/ModelTextBox.cs b/src/Util.Ui.Angular/Material/Forms/Models/ModelTextBox.cs index a073c8f3..e54f7cbb 100644 --- a/src/Util.Ui.Angular/Material/Forms/Models/ModelTextBox.cs +++ b/src/Util.Ui.Angular/Material/Forms/Models/ModelTextBox.cs @@ -39,7 +39,7 @@ namespace Util.Ui.Material.Forms.Models { /// 初始化 /// private void Init() { - Helper.InitModelControl( this, _expression, _memberInfo ); + Helper.InitControl( this, _expression, _memberInfo ); InitType(); InitValidation(); } diff --git a/src/Util.Ui.Angular/Material/Forms/Renders/SelectRender.cs b/src/Util.Ui.Angular/Material/Forms/Renders/SelectRender.cs index d157db82..9802d386 100644 --- a/src/Util.Ui.Angular/Material/Forms/Renders/SelectRender.cs +++ b/src/Util.Ui.Angular/Material/Forms/Renders/SelectRender.cs @@ -1,8 +1,10 @@ -using Util.Ui.Builders; +using Microsoft.AspNetCore.Mvc.ViewFeatures; +using Util.Ui.Builders; using Util.Ui.Configs; using Util.Ui.Material.Commons.Configs; using Util.Ui.Material.Forms.Builders; using Util.Ui.Material.Forms.Configs; +using Util.Ui.Material.Forms.Resolvers; namespace Util.Ui.Material.Forms.Renders { /// @@ -26,12 +28,23 @@ namespace Util.Ui.Material.Forms.Renders { /// 获取标签生成器 /// protected override TagBuilder GetTagBuilder() { + ResolveExpression(); var builder = new SelectWrapperBuilder(); base.Config( builder ); ConfigSelect( builder ); return builder; } + /// + /// 解析属性表达式 + /// + private void ResolveExpression() { + if( _config.Contains( UiConst.For ) == false ) + return; + var expression = _config.GetValue( UiConst.For ); + SelectExpressionResolver.Init( expression, _config ); + } + /// /// 配置 /// @@ -54,9 +67,19 @@ namespace Util.Ui.Material.Forms.Renders { /// 配置数据源 /// private void ConfigDataSource( SelectWrapperBuilder builder ) { + AddItems(); builder.AddAttribute( "[dataSource]", _config.GetValue( UiConst.DataSource ) ); } + /// + /// 添加项集合 + /// + private void AddItems() { + if( _config.Items.Count == 0 ) + return; + _config.SetAttribute( UiConst.DataSource, Util.Helpers.Json.ToJson( _config.Items, true ) ); + } + /// /// 配置重置项 /// diff --git a/src/Util.Ui.Angular/Material/Forms/Renders/TextBoxRender.cs b/src/Util.Ui.Angular/Material/Forms/Renders/TextBoxRender.cs index aa7bdf4a..b2af6fde 100644 --- a/src/Util.Ui.Angular/Material/Forms/Renders/TextBoxRender.cs +++ b/src/Util.Ui.Angular/Material/Forms/Renders/TextBoxRender.cs @@ -1,9 +1,11 @@ -using Util.Ui.Builders; +using Microsoft.AspNetCore.Mvc.ViewFeatures; +using Util.Ui.Builders; using Util.Ui.Configs; using Util.Ui.Material.Commons.Configs; using Util.Ui.Material.Enums; using Util.Ui.Material.Forms.Builders; using Util.Ui.Material.Forms.Configs; +using Util.Ui.Material.Forms.Resolvers; namespace Util.Ui.Material.Forms.Renders { /// @@ -27,6 +29,7 @@ namespace Util.Ui.Material.Forms.Renders { /// 获取标签生成器 /// protected override TagBuilder GetTagBuilder() { + ResolveExpression(); var builder = CreateBuilder(); base.Config( builder ); ConfigTextArea( builder ); @@ -35,6 +38,16 @@ namespace Util.Ui.Material.Forms.Renders { return builder; } + /// + /// 解析属性表达式 + /// + private void ResolveExpression() { + if( _config.Contains( UiConst.For ) == false ) + return; + var expression = _config.GetValue( UiConst.For ); + TextBoxExpressionResolver.Init( expression, _config ); + } + /// /// 创建标签生成器 /// @@ -130,7 +143,5 @@ namespace Util.Ui.Material.Forms.Renders { private void ConfigMaxLength( TagBuilder builder ) { builder.AddAttribute( "[maxLength]", _config.GetValue( UiConst.MaxLength ) ); } - - } } diff --git a/src/Util.Ui.Angular/Material/Forms/Resolvers/SelectExpressionResolver.cs b/src/Util.Ui.Angular/Material/Forms/Resolvers/SelectExpressionResolver.cs new file mode 100644 index 00000000..9b95e266 --- /dev/null +++ b/src/Util.Ui.Angular/Material/Forms/Resolvers/SelectExpressionResolver.cs @@ -0,0 +1,84 @@ +using System.ComponentModel.DataAnnotations; +using System.Reflection; +using Microsoft.AspNetCore.Mvc.ViewFeatures; +using Util.Helpers; +using Util.Ui.Configs; +using Util.Ui.Extensions; +using Util.Ui.Material.Commons.Internal; +using Util.Ui.Material.Forms.Configs; + +namespace Util.Ui.Material.Forms.Resolvers { + /// + /// 下拉列表表达式解析器 + /// + public class SelectExpressionResolver { + /// + /// 初始化下拉列表表达式解析器 + /// + /// 属性表达式 + /// 配置 + private SelectExpressionResolver( ModelExpression expression, SelectConfig config ) { + if( expression == null || config == null ) + return; + _expression = expression; + _config = config; + _memberInfo = expression.GetMemberInfo(); + } + + /// + /// 属性表达式 + /// + private readonly ModelExpression _expression; + + /// + /// 配置 + /// + private readonly SelectConfig _config; + + /// + /// 成员 + /// + private readonly MemberInfo _memberInfo; + + /// + /// 初始化 + /// + /// 属性表达式 + /// 配置 + public static void Init( ModelExpression expression, SelectConfig config ) { + new SelectExpressionResolver( expression, config ).Init(); + } + + /// + /// 初始化 + /// + private void Init() { + Helper.InitConfig( _config, _expression, _memberInfo ); + InitType(); + } + + /// + /// 根据类型初始化 + /// + private void InitType() { + if ( Reflection.IsBool( _memberInfo ) ) + InitBool(); + else if ( Reflection.IsEnum( _memberInfo ) ) + InitEnum(); + } + + /// + /// 初始化布尔类型 + /// + private void InitBool() { + _config.AddItems( new Item( "是", true ) ).AddItems( new Item( "否", false ) ); + } + + /// + /// 初始化枚举类型 + /// + private void InitEnum() { + _config.AddItems( Util.Helpers.Enum.GetItems( _expression.Metadata.ModelType ).ToArray() ); + } + } +} diff --git a/src/Util.Ui.Angular/Material/Forms/Resolvers/TextBoxExpressionResolver.cs b/src/Util.Ui.Angular/Material/Forms/Resolvers/TextBoxExpressionResolver.cs new file mode 100644 index 00000000..b741dcad --- /dev/null +++ b/src/Util.Ui.Angular/Material/Forms/Resolvers/TextBoxExpressionResolver.cs @@ -0,0 +1,103 @@ +using System.ComponentModel.DataAnnotations; +using System.Reflection; +using Microsoft.AspNetCore.Mvc.ViewFeatures; +using Util.Helpers; +using Util.Ui.Configs; +using Util.Ui.Extensions; +using Util.Ui.Material.Commons.Internal; +using Util.Ui.Material.Forms.Configs; + +namespace Util.Ui.Material.Forms.Resolvers { + /// + /// 文本框表达式解析器 + /// + public class TextBoxExpressionResolver { + /// + /// 初始化文本框表达式解析器 + /// + /// 属性表达式 + /// 配置 + private TextBoxExpressionResolver( ModelExpression expression, TextBoxConfig config ) { + if( expression == null || config == null ) + return; + _expression = expression; + _config = config; + _memberInfo = expression.GetMemberInfo(); + } + + /// + /// 属性表达式 + /// + private readonly ModelExpression _expression; + + /// + /// 配置 + /// + private readonly TextBoxConfig _config; + + /// + /// 成员 + /// + private readonly MemberInfo _memberInfo; + + /// + /// 初始化 + /// + /// 属性表达式 + /// 配置 + public static void Init( ModelExpression expression, TextBoxConfig config ) { + new TextBoxExpressionResolver( expression, config ).Init(); + } + + /// + /// 初始化 + /// + private void Init() { + Helper.InitConfig( _config, _expression, _memberInfo ); + InitType(); + InitValidation(); + } + + /// + /// 根据类型初始化 + /// + private void InitType() { + if( Reflection.IsNumber( _memberInfo ) ) + _config.SetAttribute( UiConst.Type, "number" ); + } + + /// + /// 初始化验证 + /// + private void InitValidation() { + InitStringLength(); + InitEmail(); + } + + /// + /// 初始化字符串长度验证 + /// + private void InitStringLength() { + var attribute = _expression.GetValidationAttribute(); + if( attribute == null ) + return; + if( attribute.MinimumLength > 0 ) + _config.SetAttribute( UiConst.MinLength, attribute.MinimumLength ); + if( attribute.MaximumLength > 0 ) + _config.SetAttribute( UiConst.MaxLength, attribute.MaximumLength ); + } + + /// + /// 初始化电子邮件验证 + /// + private void InitEmail() { + var attribute = _expression.GetValidationAttribute(); + if( attribute == null ) + return; + _config.SetAttribute( UiConst.Type, "email" ); + if ( attribute.ErrorMessage.Contains( "field is not a valid e-mail address" ) ) + return; + _config.SetAttribute( UiConst.EmailMessage, attribute.ErrorMessage ); + } + } +} diff --git a/src/Util.Ui.Angular/Material/Forms/Select.cs b/src/Util.Ui.Angular/Material/Forms/Select.cs index 8b1c7f10..e3561aae 100644 --- a/src/Util.Ui.Angular/Material/Forms/Select.cs +++ b/src/Util.Ui.Angular/Material/Forms/Select.cs @@ -39,24 +39,6 @@ namespace Util.Ui.Material.Forms { return new SelectRender( _config ); } - /// - /// 渲染前操作 - /// - /// 流写入器 - /// 编码 - protected override void RenderBefore( TextWriter writer, HtmlEncoder encoder ) { - AddItems(); - } - - /// - /// 添加项集合 - /// - private void AddItems() { - if( _config.Items.Count == 0 ) - return; - this.DataSource( Util.Helpers.Json.ToJson( _config.Items,true ) ); - } - /// /// 绑定枚举 /// diff --git a/src/Util.Ui.Angular/Material/Forms/TagHelpers/FormControlTagHelperBase.cs b/src/Util.Ui.Angular/Material/Forms/TagHelpers/FormControlTagHelperBase.cs index 70652a32..f1c43d4b 100644 --- a/src/Util.Ui.Angular/Material/Forms/TagHelpers/FormControlTagHelperBase.cs +++ b/src/Util.Ui.Angular/Material/Forms/TagHelpers/FormControlTagHelperBase.cs @@ -1,4 +1,5 @@ -using Util.Ui.Enums; +using Microsoft.AspNetCore.Mvc.ViewFeatures; +using Util.Ui.Enums; using Util.Ui.Material.Enums; using Util.Ui.TagHelpers; @@ -56,6 +57,10 @@ namespace Util.Ui.Material.Forms.TagHelpers { /// public string OnSuffixIconClick { get; set; } /// + /// 属性表达式 + /// + public ModelExpression For { get; set; } + /// /// 模型绑定 /// public string Model { get; set; } diff --git a/src/Util.Ui.Core/Configs/Config.cs b/src/Util.Ui.Core/Configs/Config.cs index 01d05f7e..42eab8c5 100644 --- a/src/Util.Ui.Core/Configs/Config.cs +++ b/src/Util.Ui.Core/Configs/Config.cs @@ -76,7 +76,7 @@ namespace Util.Ui.Configs { /// 目标类型 /// 属性名 public T GetValue( string name ) { - return Util.Helpers.Convert.To( GetValue( name ) ); + return Contains( name ) ? Util.Helpers.Convert.To( AllAttributes[name].Value ) : default(T); } /// diff --git a/src/Util.Ui.Core/Configs/UiConst.cs b/src/Util.Ui.Core/Configs/UiConst.cs index 3ab8b9fe..cc74b119 100644 --- a/src/Util.Ui.Core/Configs/UiConst.cs +++ b/src/Util.Ui.Core/Configs/UiConst.cs @@ -183,5 +183,9 @@ /// 垂直 /// public const string Vertical = "vertical"; + /// + /// for + /// + public const string For = "for"; } } diff --git a/src/Util.Ui.Core/Extensions/Extensions.ModelExpression.cs b/src/Util.Ui.Core/Extensions/Extensions.ModelExpression.cs new file mode 100644 index 00000000..8fb73cf2 --- /dev/null +++ b/src/Util.Ui.Core/Extensions/Extensions.ModelExpression.cs @@ -0,0 +1,30 @@ +using System.Linq; +using System.Reflection; +using Microsoft.AspNetCore.Mvc.ViewFeatures; + +namespace Util.Ui.Extensions { + /// + /// 模型表达式扩展 + /// + public static partial class Extensions { + /// + /// 获取成员 + /// + /// 模型表达式 + public static MemberInfo GetMemberInfo( this ModelExpression expression ) { + var members = expression.Metadata.ContainerType.GetMember( expression.Metadata.PropertyName ); + return members.Length == 0 ? null : members[0]; + } + + /// + /// 获取验证特性 + /// + /// 模型表达式 + public static TAttribute GetValidationAttribute( this ModelExpression expression ) { + var attribute = expression.Metadata.ValidatorMetadata.FirstOrDefault( t => t is TAttribute ); + if( attribute == null ) + return default( TAttribute ); + return (TAttribute)attribute; + } + } +} diff --git a/src/Util/Helpers/Enum.cs b/src/Util/Helpers/Enum.cs index 9a2e0705..9e75245b 100644 --- a/src/Util/Helpers/Enum.cs +++ b/src/Util/Helpers/Enum.cs @@ -89,15 +89,23 @@ namespace Util.Helpers { } /// - /// 获取描述项集合,文本设置为Description,值为Value + /// 获取项集合,文本设置为Description,值为Value /// /// 枚举类型 public static List GetItems() { - TypeInfo enumType = Common.GetType().GetTypeInfo(); + return GetItems( Common.GetType() ); + } + + /// + /// 获取项集合,文本设置为Description,值为Value + /// + /// 枚举类型 + public static List GetItems( Type type ) { + TypeInfo enumType = type.GetTypeInfo(); ValidateEnum( enumType ); var result = new List(); foreach( var field in enumType.GetFields() ) - AddItem( result, field ); + AddItem( type, result, field ); return result.OrderBy( t => t.SortId ).ToList(); } @@ -112,10 +120,10 @@ namespace Util.Helpers { /// /// 添加描述项 /// - private static void AddItem( ICollection result, FieldInfo field ) { + private static void AddItem( Type type, ICollection result, FieldInfo field ) { if( !field.FieldType.GetTypeInfo().IsEnum ) return; - var value = GetValue( field.Name ); + var value = GetValue( type, field.Name ); var description = Reflection.GetDescription( field ); result.Add( new Item( description, value, value ) ); } diff --git a/test/Util.Tests/Helpers/EnumTest.cs b/test/Util.Tests/Helpers/EnumTest.cs index 4d5fa4b5..90bf3e33 100644 --- a/test/Util.Tests/Helpers/EnumTest.cs +++ b/test/Util.Tests/Helpers/EnumTest.cs @@ -138,7 +138,7 @@ namespace Util.Tests.Helpers { } /// - /// 测试获取描述项集合 + /// 测试获取项集合 /// [Fact] public void TestGetItems() { @@ -153,7 +153,22 @@ namespace Util.Tests.Helpers { } /// - /// 测试获取描述项集合 - 可空枚举 + /// 测试获取项集合 + /// + [Fact] + public void TestGetItems_Type() { + var items = Util.Helpers.Enum.GetItems(typeof( EnumSample ) ); + Assert.Equal( 5, items.Count ); + Assert.Equal( "A", items[0].Text ); + Assert.Equal( 1, items[0].Value ); + Assert.Equal( "D4", items[3].Text ); + Assert.Equal( 4, items[3].Value ); + Assert.Equal( "E5", items[4].Text ); + Assert.Equal( 5, items[4].Value ); + } + + /// + /// 测试获取项集合 - 可空枚举 /// [Fact] public void TestGetItems_Nullable() { @@ -168,7 +183,7 @@ namespace Util.Tests.Helpers { } /// - /// 测试获取描述项集合 - 验证枚举类型 + /// 测试获取项集合 - 验证枚举类型 /// [Fact] public void TestGetItems_Validate() { diff --git a/test/Util.Ui.Tests/Material/Forms/RadioTest.cs b/test/Util.Ui.Tests/Material/Forms/RadioTest.cs index 03dceb01..0006dcf7 100644 --- a/test/Util.Ui.Tests/Material/Forms/RadioTest.cs +++ b/test/Util.Ui.Tests/Material/Forms/RadioTest.cs @@ -203,8 +203,8 @@ namespace Util.Ui.Tests.Material.Forms { public void TestBool() { var result = new String(); result.Append( "" ); Assert.Equal( result.ToString(), GetResult( _component.Bool() ) ); } diff --git a/test/Util.Ui.Tests/Material/Forms/SelectTest.cs b/test/Util.Ui.Tests/Material/Forms/SelectTest.cs index b39ac637..894badd5 100644 --- a/test/Util.Ui.Tests/Material/Forms/SelectTest.cs +++ b/test/Util.Ui.Tests/Material/Forms/SelectTest.cs @@ -335,8 +335,8 @@ namespace Util.Ui.Tests.Material.Forms { public void TestBool() { var result = new String(); result.Append( "" ); Assert.Equal( result.ToString(), GetResult( _component.Bool() ) ); }