1 //----------------------------------------------------------------
2 // Copyright (c) Microsoft Corporation. All rights reserved.
3 //----------------------------------------------------------------
5 namespace System.Activities.Presentation.View
7 using System.Diagnostics;
8 using System.Globalization;
9 using System.Windows.Data;
12 class TypeToStringValueConverter : IValueConverter
14 public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
16 Fx.Assert(targetType.Equals(typeof(string)), "TypeToStringValueConverter cannot convert a type to type " + targetType.FullName);
20 Fx.Assert(value is Type, string.Format(CultureInfo.InvariantCulture, "TypeToStringValueConverter cannot convert from type {0} to string", value.GetType().FullName));
21 Type editedType = (Type)value;
22 //handle primitive types
23 if (editedType.IsPrimitive || editedType.IsValueType ||
24 editedType == typeof(string) || editedType == typeof(object))
26 target = editedType.Name;
31 target = editedType.FullName;
37 public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
39 Fx.Assert(targetType.Equals(typeof(Type)), "TypeToStringValueConverter cannot convert string back to type " + targetType.FullName);
41 string stringValue = value as string;
42 if (!string.IsNullOrEmpty(stringValue))
44 // try to get the type from the type name
45 target = Type.GetType(stringValue, false, true);
46 //handle primitive types
49 stringValue = string.Format(CultureInfo.InvariantCulture, "System.{0}", stringValue);
50 target = Type.GetType(stringValue, false, true);
54 return Binding.DoNothing;