1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
| public class FieldConvertor
{
public enum ConvertMode { HtmlToWiki, WikiToHtml }
private static string GetTransformations(string stringToTransform, ConvertMode mode)
{
switch (mode)
{
case ConvertMode.HtmlToWiki:
stringToTransform = stringToTransform.Replace("<", "[[").Replace(">", "]]");
break;
case ConvertMode.WikiToHtml:
stringToTransform = stringToTransform.Replace("[[", "<").Replace("]]", ">");
break;
default:
break;
}
return stringToTransform;
}
public static void ConvertStringPropertiesTags(object objectToConvert, ConvertMode mode)
{
if (objectToConvert == null)
return;
var enumerable = objectToConvert as IEnumerable;
if (enumerable != null)
foreach (var item in enumerable)
{
ConvertStringPropertiesTags(item, mode);
return;
}
PropertyInfo[] properties = objectToConvert.GetType().GetProperties();
foreach (PropertyInfo property in properties)
{
object propertyValue = property.GetValue(objectToConvert, null);
if ((propertyValue as IEnumerable) != null && (propertyValue as IEnumerable) is ICollection)
ConvertStringPropertiesTags(propertyValue, mode);
if (property.PropertyType != typeof(string))
continue;
if (propertyValue != null && property.GetSetMethod() != null)
property.SetValue(objectToConvert, GetTransformations(propertyValue.ToString(), mode), null);
}
}
} |
Partager