JD2022-TU1/main/tools/framework/JD.Serialization/Converters.cs

93 lines
No EOL
3 KiB
C#

using System.ComponentModel;
using System;
using System.Linq;
using System.Globalization;
using System.Collections;
#if _COMPILE_UNUSED_CODE_
using System.Windows.Data;
#endif //_COMPILE_UNUSED_CODE_
namespace JD.Serialization
{
public class UAFObjectConverter : ExpandableObjectConverter
{
protected string GetTableInfo(object value)
{
var baseObj = value as SpecialCustomTypeDescriptor;
if (baseObj != null && baseObj.__TablePath != null)
return $"({baseObj.__TablePath})";
if (baseObj != null && baseObj.__TableListPath != null)
return $"(--{baseObj.__TableListPath}--)";
var valueList = value as IList;
return valueList != null ? valueList.Count.ToString() : string.Empty;
}
public override bool CanConvertTo(ITypeDescriptorContext context,
System.Type destinationType)
{
return base.CanConvertTo(context, destinationType);
}
public override object ConvertTo(ITypeDescriptorContext context,
CultureInfo culture,
object value,
System.Type destinationType)
{
if (destinationType == typeof(System.String))
{
return GetTableInfo(value);
}
return base.ConvertTo(context, culture, value, destinationType);
}
}
#if _COMPILE_UNUSED_CODE_
public class UAFTypeConverter : IValueConverter
{
// Credits: http://stackoverflow.com/questions/272633/add-spaces-before-capital-letters
private string AddSpacesToSentence(string text)
{
if (string.IsNullOrWhiteSpace(text))
return string.Empty;
var newText = new StringBuilder(text.Length * 2);
newText.Append(text[0]);
for (var i = 1; i < text.Length; i++)
{
if (char.IsUpper(text[i]) && text[i - 1] != ' ')
newText.Append(' ');
newText.Append(text[i]);
}
return newText.ToString();
}
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value == null) return null;
var format = value.ToString();
if (string.IsNullOrEmpty(format)) return value;
var rx = new Regex(@"UAF.(?<typeName>\w+)_Template");
var match = rx.Match(format);
if (match.Success) return AddSpacesToSentence(match.Groups["typeName"].Value);
rx = new Regex(@"UAF.(?<typeName>\w+)");
match = rx.Match(format);
if (match.Success) return AddSpacesToSentence(match.Groups["typeName"].Value);
return value;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
#endif // _COMPILE_UNUSED_CODE_
}