权限管理、用户权限系统、开源用户权限系统、信息化建设标准基础数据管理平台
代码改变世界

C#反射技术在多语言实现中的实际用处参考,让初学者学技术有个针对性【附源码】

2010-12-20 23:00  通用C#系统架构  阅读(1540)  评论(0编辑  收藏  举报

   做软件,有点儿类似铁人三项比赛?赛跑、射击、游泳?  光某个环节突出,也没多大用,需要整体能力都强,能把整体都可以搞定,才容易得到比赛的胜利,光某一环节非常优秀,也赢得不来整个比赛。

 

   这些年也还了不少公司,台资公司去过、中日合资的公司去过、国企也去过、民企也去过,想想人生也蛮有意思的。每个企业有每个企业的优点缺点,至少这些经历会让我重视程序的多语言实现功能。

   同样的一个程序卖给国人,可能只能收500元,而且这500也难收到。当时同样的程序卖给老美了,可能是500美金,那完全是不一样的状况了,所以多语言化的程序还是很有必要的。

 

   有些涉及到动态的东西,是不得不用反射技术是实现的,下面的程序就涉及到几个问题:

   1:语言包是不固定的,可能是任意多个。

   2:每个语言包里的内容是什么,事先是不知道的。

   3:静态变量有一大堆,而且将来会更多。

   4:若用比较笨的方法一个个变量赋值,可能要写死,别人看了代码也会笑死。

 

这里主要是用了一个反射的方法,进行了循环遍历变量,然后进行了动态赋值。

using System;
using System.Collections;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;

// 这里需要注意
using System.Reflection;

using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;


        
#region public static int GetLanguageResource(object targetObject) 从当前指定的语言包读取信息
        
/// <summary>
        
/// 从当前指定的语言包读取信息
        
/// </summary>
        
/// <returns>设置多语言的属性个数</returns>
        public static int GetLanguageResource(object targetObject)
        {
            
int returnValue = 0;
            
string name = string.Empty;
            Type type 
= targetObject.GetType();
            
// Type type = typeof(TargetObject);
            FieldInfo[] fieldInfo = type.GetFields(BindingFlags.FlattenHierarchy | BindingFlags.Public | BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Instance);
            FieldInfo currentFieldInfo;
            
string messages = string.Empty;
            
for (int i = 0; i < fieldInfo.Length; i++)
            {
                name 
= fieldInfo[i].Name;
                currentFieldInfo 
= fieldInfo[i];
                messages 
= ResourceManagerWrapper.Instance.Get(name);
                
if (messages.Length > 0)
                {
                    currentFieldInfo.SetValue(targetObject, messages);
                    returnValue
++;
                }
            }
            
return returnValue;
        }
        
#endregion

 

 

调用的方法如下:

        #region public static int GetLanguageResource() 从当前指定的语言包读取信息
        
/// <summary>
        
/// 从当前指定的语言包读取信息,用了反射循环遍历
        
/// </summary>
        
/// <returns></returns>
        public static int GetLanguageResource()
        {
            AppMessage appMessage 
= new AppMessage();
            
return BaseBusinessLogic.GetLanguageResource(appMessage);
        }
        
#endregion

 

 

AppMessage 的参考代码如下:

//------------------------------------------------------------
// All Rights Reserved , Copyright (C) 2010 , Jirisoft , Ltd. 
//------------------------------------------------------------

using System;
using System.Globalization;

namespace DotNet.Utilities
{
    
/// <summary>
    
///    BUBaseAppMessage
    
/// 通用消息控制基类
    
/// 
    
/// 修改纪录
    
///        2007.05.17 版本:1.0    JiRiGaLa 建立,为了提高效率分开建立了类。
    
///    
    
/// 版本:3.1
    
///
    
/// <author>
    
///        <name>JiRiGaLa</name>
    
///        <date>2007.05.17</date>
    
/// </author> 
    
/// </summary>
    public class AppMessage
    {
        
/// <summary>
        
/// 提示信息.
        
/// </summary>
        public static string MSG0000 = "提示信息";

        
/// <summary>
        
/// 发生未知错误.
        
/// </summary>
        public static string MSG0001 = "发生未知错误。";

        
/// <summary>
        
/// 数据库联接不正常.
        
/// </summary>
        public static string MSG0002 = "数据库联接不正常。";

        
/// <summary>
        
/// WebService联接不正常.
        
/// </summary>
        public static string MSG0003 = "WebService 联接不正常。";

        
/// <summary>
        
/// 任何数据未被修改.
        
/// </summary>
        public static string MSG0004 = "任何数据未被修改。";

        
/// <summary>
        
/// 记录未找到,可能已被其他人删除.
        
/// </summary>
        public static string MSG0005 = "记录未找到,可能已被其他人删除。";

        
/// <summary>
        
/// 数据已被其他人修改,请按F5键,重新刷新获得数据.
        
/// </summary>
        public static string MSG0006 = "数据已被其他人修改,请按F5键重新刷新获得数据。";

        
/// <summary>
        
/// '{O}'不允许为空,请输入.
        
/// </summary>
        public static string MSG0007 = "请输入{0} 不允许为空,请输入。";

        
/// <summary>
        
/// {0} 已存在,不可以重复.
        
/// </summary>
        public static string MSG0008 = "{0} 已存在,不可以重复。";

        
/// <summary>
        
/// 新增成功.
        
/// </summary>
        public static string MSG0009 = "新增成功。";

        
/// <summary>
        
/// 更新成功.
        
/// </summary>
        public static string MSG0010 = "更新成功。";

        
/// <summary>
        
/// 保存成功.
        
/// </summary>
        public static string MSG0011 = "保存成功。";

        
/// <summary>
        
/// 批量保存成功.
        
/// </summary>
        public static string MSG0012 = "批量保存成功。";

        
/// <summary>
        
/// 删除成功.
        
/// </summary>
        public static string MSG0013 = "删除成功。";

        
/// <summary>
        
/// 批量删除成功.
        
/// </summary>
        public static string MSG0014 = "批量删除成功。";

        
/// <summary>
        
/// 您确认删除吗?
        
/// </summary>
        public static string MSG0015 = "您确认删除吗?";

        
/// <summary>
        
/// 您确认删除 '{0}'吗?
        
/// </summary>
        public static string MSG0016 = "您确认删除 {0} 吗?";

        
/// <summary>
        
/// 当前记录不允许被删除.
        
/// </summary>
        public static string MSG0017 = "当前记录不允许被删除。";

        
/// <summary>
        
/// 当前记录 '{0}' 不允许被删除.
        
/// </summary>
        public static string MSG0018 = "当前记录 {0} 不允许被删除。";

        
/// <summary>
        
/// 当前记录不允许被编辑,请按F5键,重新获取数据最新数据.
        
/// </summary>
        public static string MSG0019 = "当前记录不允许被编辑,请按F5键,重新获取数据最新数据。";

        
/// <summary>
        
/// 当前记录 '{0}' 不允许被编辑,请按F5键,重新获取数据最新数据.
        
/// </summary>
        public static string MSG0020 = "当前记录 {0} 不允许被编辑,请按F5键,重新获取数据最新数据。";

        
/// <summary>
        
/// 当前记录已是第一条记录.
        
/// </summary>
        public static string MSG0021 = "当前记录已是第一条记录。";

        
/// <summary>
        
/// 当前记录已是最后一条记录.
        
/// </summary>
        public static string MSG0022 = "当前记录已是最后一条记录。";

        
/// <summary>
        
/// 请至少选择一项.
        
/// </summary>
        public static string MSG0023 = "请选择一条记录。";

        
/// <summary>
        
/// 请至少选择一项 '{0}'.
        
/// </summary>
        public static string MSG0024 = "请至少选择一项 '{0}'。";

        
/// <summary>
        
/// '{0}'不能大于'{1}'.
        
/// </summary>
        public static string MSG0025 = "{0} 不能大于{1}。";

        
/// <summary>
        
/// '{0}'不能小于'{1}'.
        
/// </summary>
        public static string MSG0026 = "{0} 不能小于 {1}。";

        
/// <summary>
        
/// '{0}'不能等于'{1}'.
        
/// </summary>
        public static string MSG0027 = "{0} 不能等于 {1}。";

        
/// <summary>
        
/// 输入的'{0}'不是有效的日期.
        
/// </summary>
        public static string MSG0028 = "输入的 {0} 不是有效的日期。";

        
/// <summary>
        
/// 输入的'{0}'不是有效的字符.
        
/// </summary>
        public static string MSG0029 = "输入的 {0} 不是有效的字符。";

        
/// <summary>
        
/// 输入的'{0}'不是有效的数字.
        
/// </summary>
        public static string MSG0030 = "输入的 {0} 不是有效的数字。";

        
/// <summary>
        
/// 输入的'{0}'不是有效的金额.
        
/// </summary>
        public static string MSG0031 = "输入的 {0} 不是有效的金额。";

        
/// <summary>
        
/// '{0}'名不能包含:\ / : * ? " < > |
        
/// </summary>
        public static string MSG0032 = "{0} 名包含非法字符。";

        
/// <summary>
        
/// 数据已经被引用,有关联数据在
        
/// </summary>
        public static string MSG0033 = "数据已经被引用,有关联数据在。";

        
/// <summary>
        
/// 数据已经被引用,有关联数据在.是否强制删除数据?
        
/// </summary>
        public static string MSG0034 = "数据已经被引用,有关联数据在,是否强制删除数据?";

        
/// <summary>
        
/// {0} 有子节点不允许被删除.
        
/// </summary>
        public static string MSG0035 = "{0} 有子节点不允许被删除,有子节点还未被删除。";

        
/// <summary>
        
/// {0} 不能移动到 {1}.
        
/// </summary>
        public static string MSG0036 = "{0} 不能移动到 {1}。";

        
/// <summary>
        
/// {0} 下的子节点不能移动到 {1}.
        
/// </summary>
        public static string MSG0037 = "{0} 下的子节点不能移动到 {1}。";

        
/// <summary>
        
/// 确认移动 {0} 到 {1} 吗?
        
/// </summary>
        public static string MSG0038 = "确认移动 {0} 到 {1} 吗?";

        
/// <summary>
        
/// '{0}'不等于'{1}'.
        
/// </summary>
        public static string MSG0039 = "{0} 不等于 {1}。";

        
/// <summary>
        
/// {0} 错误.
        
/// </summary>
        public static string MSG0040 = "{0} 错误。";

        
/// <summary>
        
/// 确认审核通过吗?.
        
/// </summary>
        public static string MSG0041 = "确认审核通过吗?";

        
/// <summary>
        
/// 确认驳回吗?.
        
/// </summary>
        public static string MSG0042 = "确认审核驳回吗?";

        
/// <summary>
        
/// 成功锁定数据.
        
/// </summary>
        public static string MSG0043 = "不能锁定数据。";

        
/// <summary>
        
/// 不能锁定数据.
        
/// </summary>
        public static string MSG0044 = "成功锁定数据。";

        
/// <summary>
        
/// 数据被修改提示
        
/// </summary>
        public static string MSG0045 = "数据已经改变,想保存数据吗?";

        
/// <summary>
        
/// 最近 {0} 次内密码不能重复。
        
/// </summary>
        public static string MSG0046 = "最近 {0} 次内密码不能重复。";

        
/// <summary>
        
/// 密码已过期,账户被锁定,请联系系统管理员。
        
/// </summary>
        public static string MSG0047 = "密码已过期,账户被锁定,请联系系统管理员。";

        
/// <summary>
        
/// 数据已经改变,不保存数据?
        
/// </summary>
        public static string MSG0065 = "数据已经改变,不保存数据?";

        
public static string MSG0048 = "拒绝登录,用户已经在线上。";
        
public static string MSG0049 = "拒绝登录,网卡Mac地址不符限制条件。";
        
public static string MSG0050 = "拒绝登录,IP地址不符限制条件";
        
public static string MSG0051 = "已到在线用户最大数量限制。";


        
public static string MSG0060 = "请先创建该职员的登录系统的用户信息。";

        
/// <summary>
        
/// 您确认移除吗?
        
/// </summary>
        public static string MSG0075 = "您确认移除吗?";

        
/// <summary>
        
/// 您确认移除 '{0}'吗?
        
/// </summary>
        public static string MSG0076 = "您确认移除 {0} 吗?";

        
public static string MSG0077 = "成功删除 {0} 条记录。";

        
public static string MSG0700 = "已经成功连接到目标数据。";

        
public static string MSG9800 = "";
        
public static string MSG9900 = "公司";
        
public static string MSG9901 = "部门";

        
public static string MSG9910 = "用户未设置电子邮件地址。"// UserNotEmail
        public static string MSG9911 = "用户被锁定。"// UserLocked
        public static string MSG9912 = "用户还未激活账户。"// UserNotActive
        public static string MSG9913 = "用户账户已被激活。"// UserIsActivate
        
        
public static string MSG9956 = "未找到满足条件的记录。";
        
public static string MSG9957 = "用户名";
        
public static string MSG9958 = "数据验证错误。";
        
public static string MSG9959 = "新密码";
        
public static string MSG9960 = "确认密码";
        
public static string MSG9961 = "原密码";
        
public static string MSG9962 = "修改 {0} 成功。";
        
public static string MSG9963 = "设置 {0} 成功。";
        
public static string MSG9964 = "密码";
        
public static string MSG9965 = "登录成功。";
        
public static string MSG9966 = "用户没有找到,请注意大小写。";
        
public static string MSG9967 = "密码错误,请注意大小写。";
        
public static string MSG9968 = "登录被拒绝,请与管理员联系。";
        
public static string MSG9969 = "基础编码";
        
public static string MSG9970 = "职员";
        
public static string MSG9971 = "组织机构";
        
public static string MSG9972 = "角色";
        
public static string MSG9973 = "模块";
        
public static string MSG9974 = "文件夹";
        
public static string MSG9975 = "权限";
        
public static string MSG9976 = "主键";
        
public static string MSG9977 = "编号";
        
public static string MSG9978 = "名称";
        
public static string MSG9979 = "父节点主键";
        
public static string MSG9980 = "父节点名称";
        
public static string MSG9981 = "功能分类主键";
        
public static string MSG9982 = "唯一识别主键";
        
public static string MSG9983 = "主题";
        
public static string MSG9984 = "内容";
        
public static string MSG9985 = "状态码";
        
public static string MSG9986 = "次数";
        
public static string MSG9987 = "有效";
        
public static string MSG9988 = "备注";
        
public static string MSG9989 = "排序码";
        
public static string MSG9990 = "创建者主键";
        
public static string MSG9991 = "创建时间";
        
public static string MSG9992 = "最后修改者主键";
        
public static string MSG9993 = "最后修改时间";
        
public static string MSG9994 = "排序";
        
public static string MSG9995 = "主键";
        
public static string MSG9996 = "索引";
        
public static string MSG9997 = "字段";
        
public static string MSG9998 = "";
        
public static string MSG9999 = "数据库";

        
//韩峰修改20101106
        public static string MSG0201 = "温馨提示:您选择的文件不存在,请重新选择。";
        
public static string MSG0202 = "提示信息";
        
public static string MSG0203 = "您确认移动 \" {0} \" 到 \" {1} \" 吗?" ;
        
public static string MSG0204 = "您确认退出应用程序吗?";
        
public static string MSG0205 = "文件 {0} 已存在,要覆盖服务器上的文件吗?";
        
public static string MSG0206 = "已经超过了上传限制,请检查要上传的文件大小。";
        
public static string MSG0207 = "您确认要删除图片吗?";
        
public static string MSG0208 = "开始时间不能大于结束时间。";
        
public static string MSG0209 = "清除成功。";
        
public static string MSG0210 = "重置成功。";
        
public static string MSG0211 = "已输入{0}次错误密码,不再允许继续登录,请重新启动程序进行登录。";
        
public static string MSG0212 = "查询内容";
        
public static string MSG0213 = "编号总长度不要超过40位。";
        
public static string MSG0214 = "编号生成成功。";
        
public static string MSG0215 = "增序列";
        
public static string MSG0216 = "减序列";
        
public static string MSG0217 = "步调";
        
public static string MSG0218 = "序列重置成功。";
        
public static string MSG0219 = "您确认重置序列吗?";
        
public static string MSG0223 = "用户名 不允许为空,请输入。";
        
public static string MSG0225 = "目前节点上有数据。";
        
public static string MSG0226 = "无法删除自己";
        
public static string MSG0228 = "设置关联用户(账户)成功。";
        
public static string MSG0229 = "所在单位 不允许为空,请选择。";
        
public static string MSG0230 = "申请账户更新成功,请等待审核。";
        
public static string MSG0231 = "密码 不等于 确认密码,请确认后重新输入。";
        
public static string MSG0232 = "用户名";
        
public static string MSG0233 = "姓名";
        
public static string MSG0234 = "E_mail 格式不正确,请重新输入。";
        
public static string MSG0235 = "申请账户成功,请等待审核。";
        
public static string MSG0236 = "导出的目标文件已存在要覆盖 \"{0}\" 吗?";
        
public static string MSG0237 = "成功发送电子邮件。";
        
public static string MSG0238 = "清除异常信息成功。";
        
public static string MSG0239 = "您确认清除异常信息吗?";
        
public static string MSG0240 = "内容不能为空";
        
public static string MSG0241 = "发送邮件失败.";
        
public static string MSG0242 = "移动成功。";

        
public static string MSG0245 = "用户、角色必须选择一个。";

        
public static string MSG0247 = "审批通过 {0}项。";
        
public static string MSG0248 = "审批通过失败。";
        
public static string MSG0249 = "请选需要处理的数据。";
        
public static string MSG0250 = "您确认审批通过此单据吗?";
        
public static string MSG0251 = "成功驳回单据。";
        
public static string MSG0252 = "请选需要处理的数据。";
        
public static string MSG0253 = "您确认不输入驳回理由吗?";
        
public static string MSG0255 = "您确认驳回此单据吗?";
        
public static string MSG0256 = "工作流程发送成功。";
        
public static string MSG0257 = "工作流程发送失败。";
        
public static string MSG0258 = "审批通过单据。";
        
public static string MSG0259 = "请选需要处理的数据。";
        
public static string MSG0260 = "请选择递交给哪位用户。";
        
public static string MSG0261 = "最终审批通过 {0}项。";
        
public static string MSG0262 = "最终审批失败。";
        
public static string MSG0263 = "请选需要处理的数据。";
        
public static string MSG0264 = "成功驳回单据。";
        
public static string MSG0265 = "请选需要处理的数据。";
        
public static string MSG0266 = "您确认不输入驳回理由吗?";
        
public static string MSG0267 = "您确认驳回此单据吗?";
        
public static string MSG0268 = "审批流程发送成功。";
        
public static string MSG0269 = "请选需要处理的数据。";
        
public static string MSG0270 = "请选择递交给哪位用户。";
        
public static string MSG0271 = "您确认递交给 {0}吗?";
        
public static string MSG0272 = "审批流程撤消成功{0}项。";
        
public static string MSG0273 = "审批流程撤消失败。";
        
public static string MSG0274 = "请选需要处理的数据。";
        
public static string MSG0275 = "您确认不输入驳回理由吗?";
        
public static string MSG0276 = "您确认撤销撤消审批流程中的单据吗?";
        
public static string MSG0277 = "请选择递交给哪个用户审批。";
        
public static string MSG0278= "您确认递交给用户 {0}审批吗?";
        
public static string MSG0279 = "工作流程发送成功。";
        
public static string MSG0280 = "工作流程发送失败。";
        
public static string MSG0281 = "您确认替换文件 {0} 吗?";
        
public static string MSG0282 = "上级机构";
        
public static string MSG0283 = "编号生成成功";
        
public static string MSG0284 = "已修改配置信息,需要保存吗?";


        
//重新登录时 登录窗体名称改变为“重新登录”
        public static string MSGReLogOn = "重新登录";

        
#region public static int GetLanguageResource() 从当前指定的语言包读取信息
        
/// <summary>
        
/// 从当前指定的语言包读取信息,用了反射循环遍历
        
/// </summary>
        
/// <returns></returns>
        public static int GetLanguageResource()
        {
            AppMessage appMessage 
= new AppMessage();
            
return BaseBusinessLogic.GetLanguageResource(appMessage);
        }
        
#endregion

        
#region public static string Format(string value, params string[] messages) 格式化一个资源字符串
        
/// <summary>
        
/// 格式化一个资源字符串
        
/// </summary>
        
/// <param name="value">目标字符串</param>
        
/// <param name="messages">传入的信息</param>
        
/// <returns>字符串</returns>
        public static string Format(string value, params string[] messages)
        {
            
return String.Format(CultureInfo.CurrentCulture, value, messages);
        }
        
#endregion

        
#region public static string GetMessage(string id) 读取一个资源定义
        
/// <summary>
        
/// 读取一个资源定义
        
/// </summary>
        
/// <param name="id">资源主键</param>
        
/// <returns>字符串</returns>
        public static string GetMessage(string id)
        {
            
string returnValue = string.Empty;
            returnValue 
= ResourceManagerWrapper.Instance.Get(id);
            
return returnValue;
        }
        
#endregion

        
#region public static string GetMessage(string id, params string[] messages)
        
/// <summary>
        
/// 读取一个资源定义
        
/// </summary>
        
/// <param name="id">资源主键</param>
        
/// <param name="messages">传入的信息</param>
        
/// <returns>字符串</returns>
        public static string GetMessage(string id, params string[] messages)
        {
            
string returnValue = string.Empty;
            returnValue 
= ResourceManagerWrapper.Instance.Get(id);
            returnValue 
= String.Format(CultureInfo.CurrentCulture, returnValue, messages);
            
return returnValue;
        }
        
#endregion
    }
}

 

 

 

 

 



C# ASP.NET 通用权限设计、通用权限管理、通用权限组件、单点登录、集中式权限管理、统一授权体系、分级管理分级授权


微信扫一扫加好友