第一步创建元数据类

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Reflection;
 5 using System.Web;
 6 using System.Web.Mvc;
 7 
 8 namespace MvcApplication19
 9 {
10     [AttributeUsage(AttributeTargets.Class | AttributeTargets.Property)]
11     public class DisplayTextAttribute : Attribute, IMetadataAware
12     {
13         private static Type staticResourceType;
14         public string DisplayName { get; set; }
15         public Type ResourceType { get; set; }
16 
17         public DisplayTextAttribute()
18         {
19             this.ResourceType = staticResourceType;
20         }
21 
22         public void OnMetadataCreated(ModelMetadata metadata)
23         {
24             this.DisplayName = this.DisplayName ?? (metadata.PropertyName ?? metadata.ModelType.Name);
25             if (null == this.ResourceType)
26             {
27                 metadata.DisplayName = this.DisplayName;
28                 return;
29             }
30             PropertyInfo property = this.ResourceType.GetProperty(this.DisplayName, BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static);
31             metadata.DisplayName = property.GetValue(null, null).ToString();
32         }
33 
34         public static void SetResourceType(Type resourceType)
35         {
36             staticResourceType = resourceType;
37         }
38     }
39 }
View Code

相关文章:

  • 2022-02-05
  • 2021-11-19
  • 2022-12-23
  • 2021-12-13
  • 2021-08-28
  • 2021-11-07
  • 2021-05-23
  • 2021-09-21
猜你喜欢
  • 2021-07-08
  • 2022-03-04
  • 2021-07-29
  • 2022-03-09
  • 2021-09-15
  • 2022-12-23
相关资源
相似解决方案