码迷,mamicode.com
首页 > 其他好文 > 详细

如何使用Resource资源文件

时间:2017-04-26 22:23:10      阅读:848      评论:0      收藏:0      [点我收藏+]

标签:val   typeof   type   images   res   erro   height   number   显示   

一、目的

       为了能够在DisplayAttribute中重复使用同样的名称,将所有的显示字符串集中管理。

二、方法

        1、DisplayAttribute本身支持直接使用资源文件。

[Display(ResourceType = typeof(Resource2), Name = "StudentName")]

  ResourceType为资源文件名,表示存在一个名称为"Resource2"的资源文件,其中有一个Key的名称为"StudentName"

    public class Student
    {
        [Required(ErrorMessageResourceType = typeof(Resource2), ErrorMessageResourceName = "StudentName")]
        [Display(ResourceType = typeof(Resource2), Name = "StudentName")]

        public virtual string Name { get; set; }

        [Display(ResourceType = typeof(Resource2), Name = "SortNumber")]
        public virtual long SortNumber { get; set; }

        [Display(ResourceType = typeof(Resource2), Name = "Remarks")]
        public virtual string Remarks { get; set; }
    }

  

     2、在Visual Studio 2017中直接创建一个Resource2的资源文件,后缀为"*.resx"

技术分享

    3、在代码中可直接使用Resource2,获取其中的key/value

string studentName = Resource2.StudentName;

  

如何使用Resource资源文件

标签:val   typeof   type   images   res   erro   height   number   显示   

原文地址:http://www.cnblogs.com/hahaxi/p/6771318.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!