/// <summary> /// Mapping current language /// </summary> /// <typeparam name="T"></typeparam> /// <typeparam name="R"></typeparam> /// <param name="obj"></param> /// <param name="fields"></param> /// <returns></returns> public static R ConvertObjectbyLanguage <T, R>(T obj, params string[] fields) where T : class where R : new() { object objRes = null; if (typeof(T) != typeof(R)) { R nR = CloneObject <T, R>(obj); objRes = nR; } else { objRes = obj; } try { CommonUtil.LANGUAGE_LIST lang = CurrentLanguage(); foreach (string field in fields) { PropertyInfo propEN = typeof(T).GetProperty(field + CommonValue.LANGUAGE_EN); PropertyInfo propJP = typeof(T).GetProperty(field + CommonValue.LANGUAGE_JP); PropertyInfo propLC = typeof(T).GetProperty(field + CommonValue.LANGUAGE_LC); if (objRes != null) { PropertyInfo prop = objRes.GetType().GetProperty(field); if (prop != null) { object val = null; if (propEN != null && lang == LANGUAGE_LIST.LANGUAGE_1 || (propJP == null && lang == LANGUAGE_LIST.LANGUAGE_2)) { val = propEN.GetValue(obj, null); } else if (propJP != null && lang == LANGUAGE_LIST.LANGUAGE_2) { val = propJP.GetValue(obj, null); } else if (propLC != null) { val = propLC.GetValue(obj, null); } if (val != null) { prop.SetValue(objRes, val, null); } } } } } catch (Exception ex) { throw ex; } return((R)objRes); }
/// <summary> /// Mapping current language /// </summary> /// <param name="obj"></param> public static void MappingObjectLanguage(object obj) { try { if (obj != null) { CommonUtil.LANGUAGE_LIST lang = CurrentLanguage(); Dictionary <string, LanguageMappingAttribute> langAttr = CreateAttributeDictionary <LanguageMappingAttribute>(obj); foreach (KeyValuePair <string, LanguageMappingAttribute> attr in langAttr) { PropertyInfo prop = obj.GetType().GetProperty(attr.Key); if (prop != null) { if (lang == LANGUAGE_LIST.LANGUAGE_1 || lang == LANGUAGE_LIST.LANGUAGE_2) { PropertyInfo propEN = obj.GetType().GetProperty(prop.Name + CommonValue.LANGUAGE_EN); PropertyInfo propJP = obj.GetType().GetProperty(prop.Name + CommonValue.LANGUAGE_JP); if (propJP != null && lang == LANGUAGE_LIST.LANGUAGE_2) { prop.SetValue(obj, propJP.GetValue(obj, null), null); } else if (propEN != null) { prop.SetValue(obj, propEN.GetValue(obj, null), null); } } else { PropertyInfo propLC = obj.GetType().GetProperty(prop.Name + CommonValue.LANGUAGE_LC); if (propLC != null) { prop.SetValue(obj, propLC.GetValue(obj, null), null); } } } } } } catch (Exception) { throw; } }
/// <summary> /// Mapping current language /// </summary> /// <typeparam name="T"></typeparam> /// <typeparam name="R"></typeparam> /// <param name="lst"></param> /// <param name="fields"></param> /// <returns></returns> public static List <R> ConvertObjectbyLanguage <T, R>(List <T> lst, params string[] fields) where T : class where R : new() { List <R> nLst = null; if (typeof(T) != typeof(R)) { nLst = new List <R>(); } try { if (lst != null) { CommonUtil.LANGUAGE_LIST lang = CurrentLanguage(); foreach (T ilst in lst) { object obj = null; foreach (string field in fields) { PropertyInfo propEN = typeof(T).GetProperty(field + CommonValue.LANGUAGE_EN); PropertyInfo propJP = typeof(T).GetProperty(field + CommonValue.LANGUAGE_JP); PropertyInfo propLC = typeof(T).GetProperty(field + CommonValue.LANGUAGE_LC); if (nLst != null) { if (obj == null) { /* --- Clone --- */ R nR = CloneObject <T, R>(ilst); nLst.Add(nR); obj = nR; } } else { obj = ilst; } if (obj != null) { PropertyInfo prop = obj.GetType().GetProperty(field); if (prop != null) { object val = null; if (propEN != null && lang == LANGUAGE_LIST.LANGUAGE_1 || (propJP == null && lang == LANGUAGE_LIST.LANGUAGE_2)) { val = propEN.GetValue(ilst, null); } else if (propJP != null && lang == LANGUAGE_LIST.LANGUAGE_2) { val = propJP.GetValue(ilst, null); } else if (propLC != null) { val = propLC.GetValue(ilst, null); } if (val != null) { prop.SetValue(obj, val, null); } } } } } } } catch (Exception ex) { throw ex; } if (nLst != null) { return(nLst); } else { return(lst as List <R>); } }