首页 > 其他 > 详细

如何使用Resource资源文件

时间:2017-04-26 22:16:52      阅读:319      评论:0      收藏:0      [点我收藏+]

一、目的

       为了能够在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资源文件

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!