using ANX.Framework.Build;
using ANX.Framework.VisualStudio.Nodes;
using ANX.Framework.VisualStudio.PropertyDescriptors;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Globalization;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
namespace ANX.Framework.VisualStudio.Converters
{
///
/// A TypeConverter that gets used as a proxy between the real converter and visual studio.
///
public class WrappedConverter : StringConverter
{
private class Proxy : MarshalByRefObject, IProxy
{
TypeConverter innerConverter;
Type propertyType;
public override object InitializeLifetimeService()
{
return null;
}
public void Initialize(Type converterType, Type propertyType)
{
if (converterType == null)
throw new ArgumentNullException("converterType");
if (propertyType == null)
throw new ArgumentNullException("propertyType");
this.propertyType = propertyType;
if (propertyType.IsEnum)
{
this.innerConverter = new EnumConverter(propertyType);
}
else
{
var parameterTypes = new Type[] { typeof(Type) };
ConstructorInfo constructor = converterType.GetConstructor(parameterTypes);
if (constructor != null)
{
this.innerConverter = (TypeConverter)TypeDescriptor.CreateInstance(null, converterType, parameterTypes, new object[] { propertyType });
}
else
{
this.innerConverter = (TypeConverter)TypeDescriptor.CreateInstance(null, converterType, null, null);
}
if (this.innerConverter == null)
{
throw new InvalidOperationException(string.Format("Unable to create TypeConverter for {0}", converterType.FullName));
}
}
}
public void Initialize(TypeConverter converter)
{
if (converter == null)
throw new ArgumentNullException("converter");
this.innerConverter = converter;
}
public void Initialize(string converterType, string propertyType)
{
bool buildAppDomain = AppDomain.CurrentDomain.IsBuildAppDomain();
Initialize(Type.GetType(converterType, true), Type.GetType(propertyType, true));
}
public string ConverterTypeName
{
get { return innerConverter.GetType().AssemblyQualifiedName; }
}
public bool CanConvertFrom(IProxy context, Type sourceType)
{
return innerConverter.CanConvertFrom(new TypeDescriptorContextWrapper(context), sourceType);
}
public bool CanConvertTo(IProxy context, Type destinationType)
{
return innerConverter.CanConvertTo(new TypeDescriptorContextWrapper(context), destinationType);
}
public object ConvertTo(IProxy context, CultureInfo culture, object value, Type destinationType)
{
TypeDescriptorContextWrapper wrappedContext = null;
if (context != null)
{
wrappedContext = new TypeDescriptorContextWrapper(context);
}
if (value is string && propertyType != destinationType)
{
value = innerConverter.ConvertFromString((string)value);
}
return innerConverter.ConvertTo(wrappedContext, culture, value, destinationType);
}
public object ConvertFrom(IProxy context, CultureInfo culture, object value, bool convertToString)
{
TypeDescriptorContextWrapper wrappedContext = null;
if (context != null)
{
wrappedContext = new TypeDescriptorContextWrapper(context);
}
object result = innerConverter.ConvertFrom(wrappedContext, culture, value);
if (convertToString)
{
result = innerConverter.ConvertToString(wrappedContext, culture, result);
}
return result;
}
public string CreateInstance(IProxy context, Dictionary