private static void LocalizeProperties <T>(T item, string language) { var properties = item .GetType() .GetProperties() .Where(i => Attribute.IsDefined(i, typeof(LocalizedAttribute))); foreach (var propertyInfo in properties) { var propertyValue = propertyInfo.GetValue(item)?.ToString(); if (string.IsNullOrEmpty(propertyValue)) { continue; } LocalizedContent[] localizedContents; if (!LocalizedContent.TryDeserialize(propertyValue, out localizedContents)) { continue; } var contentForLanguage = GetContentForLanguage(localizedContents, language); propertyInfo.SetValue(item, contentForLanguage.Value, null); } }
private static void LocalizeProperties <T>(T item, string language, IEnumerable <Expression <Func <T, string> > > properties) where T : class, ILocalizable { foreach (var property in properties) { var memberExpression = (MemberExpression)property.Body; var propertyValue = property.Compile()(item); if (string.IsNullOrEmpty(propertyValue)) { continue; } LocalizedContent[] localizedContents; if (!LocalizedContent.TryDeserialize(propertyValue, out localizedContents)) { continue; } var contentForLanguage = GetContentForLanguage(localizedContents, language); var propertyInfo = (PropertyInfo)memberExpression.Member; propertyInfo.SetValue(item, contentForLanguage.Value, null); } }