1 //----------------------------------------------------------------
2 // Copyright (c) Microsoft Corporation. All rights reserved.
3 //----------------------------------------------------------------
4 namespace System.Activities.Presentation.Internal.PropertyEditing.Editors
7 using System.Collections;
8 using System.Globalization;
9 using System.Reflection;
10 using System.Activities.Presentation.PropertyEditing;
11 using System.Activities.Presentation.Internal.PropertyEditing.FromExpression.Framework.PropertyInspector;
14 // Collection of utilities used by the various value editors
16 internal static class EditorUtilities
19 // Key = Type, Value = bool
20 private static Hashtable _cachedLookups = new Hashtable();
23 // Checks to see whether the specified Type is concrete and has a default constructor.
24 // That information if both returned and cached for future reference.
26 // NOTE: This method does not handle structs correctly because it will return FALSE
27 // for struct types, which is incorrect. However, this bug has its counter-part in
28 // System.Activities.Presentation.dll where the default NewItemFactory only instantiates
29 // non-struct classes. Both of these need to be fixed at the same time because
30 // they are used in conjunction. However, MWD is currently locked.
33 // <param name="type">Type to verify</param>
34 // <returns>True if the specified type is concrete and has a default constructor,
35 // false otherwise.</returns>
36 public static bool IsConcreteWithDefaultCtor(Type type)
39 object returnValue = _cachedLookups[type];
40 if (returnValue == null)
42 if (type == null || type.IsAbstract)
48 ConstructorInfo defaultCtor = type.GetConstructor(Type.EmptyTypes);
49 returnValue = (defaultCtor != null && defaultCtor.IsPublic);
52 _cachedLookups[type] = returnValue;
55 return (bool)returnValue;
59 // Substitutes user-friendly display names for values of properties
61 // <param name="item">Item to attempt to identify</param>
62 // <returns>String value for the item (guaranteed to be non-null)</returns>
63 public static string GetDisplayName(object item)
70 // Display a user-friendly string for PropertyValues
71 PropertyValue propertyValue = item as PropertyValue;
72 if (propertyValue != null)
74 return PropertyValueToDisplayNameConverter.Instance.Convert(
75 propertyValue, typeof(string), null, CultureInfo.CurrentCulture).ToString();
78 // Display a user-friendly string for NewItemFactoryTypeModels
79 NewItemFactoryTypeModel model = item as NewItemFactoryTypeModel;
82 return NewItemFactoryTypeModelToDisplayNameConverter.Instance.Convert(
83 model, typeof(string), null, CultureInfo.CurrentCulture).ToString();
86 // Otherwise, resort to ToString() implementation
87 return item.ToString();
91 // Tests whether a type t is a nullable enum type
93 // <param name="t">The type object to be tested</param>
94 // <returns>A bool indicating the test result</returns>
95 public static bool IsNullableEnumType(Type t)
97 if (t.IsGenericType && t.GetGenericTypeDefinition() == typeof(Nullable<>))
99 Type[] genericArgs = t.GetGenericArguments();
100 if (genericArgs != null && genericArgs.Length == 1)
102 return genericArgs[0].IsEnum;
109 public const string NullString = "(null)";