[reflection] Coop handles icalls in System.Reflection and System.RuntimeTypeHandle...
[mono.git] / mcs / class / referencesource / System.Activities.Presentation / System.Activities.Presentation / System / Activities / Presentation / Base / Core / Internal / PropertyEditing / Editors / EditorUtilities.cs
1 //----------------------------------------------------------------
2 // Copyright (c) Microsoft Corporation.  All rights reserved.
3 //----------------------------------------------------------------
4 namespace System.Activities.Presentation.Internal.PropertyEditing.Editors 
5 {
6     using System;
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;
12
13     // <summary>
14     // Collection of utilities used by the various value editors
15     // </summary>
16     internal static class EditorUtilities 
17     {
18
19         // Key = Type, Value = bool
20         private static Hashtable _cachedLookups = new Hashtable();
21
22         // <summary>
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.
25         //
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.
31         //
32         // </summary>
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) 
37         {
38
39             object returnValue = _cachedLookups[type];
40             if (returnValue == null) 
41             {
42                 if (type == null || type.IsAbstract) 
43                 {
44                     returnValue = false;
45                 }
46                 else 
47                 {
48                     ConstructorInfo defaultCtor = type.GetConstructor(Type.EmptyTypes);
49                     returnValue = (defaultCtor != null && defaultCtor.IsPublic);
50                 }
51
52                 _cachedLookups[type] = returnValue;
53             }
54
55             return (bool)returnValue;
56         }
57
58         // <summary>
59         // Substitutes user-friendly display names for values of properties
60         // </summary>
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) 
64         {
65             if (item == null)
66             {
67                 return string.Empty;
68             }
69
70             // Display a user-friendly string for PropertyValues
71             PropertyValue propertyValue = item as PropertyValue;
72             if (propertyValue != null)
73             {
74                 return PropertyValueToDisplayNameConverter.Instance.Convert(
75                     propertyValue, typeof(string), null, CultureInfo.CurrentCulture).ToString();
76             }
77
78             // Display a user-friendly string for NewItemFactoryTypeModels
79             NewItemFactoryTypeModel model = item as NewItemFactoryTypeModel;
80             if (model != null)
81             {
82                 return NewItemFactoryTypeModelToDisplayNameConverter.Instance.Convert(
83                     model, typeof(string), null, CultureInfo.CurrentCulture).ToString();
84             }
85
86             // Otherwise, resort to ToString() implementation
87             return item.ToString();
88         }
89
90         // <summary>
91         // Tests whether a type t is a nullable enum type
92         // </summary>
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)
96         {
97             if (t.IsGenericType && t.GetGenericTypeDefinition() == typeof(Nullable<>))
98             {
99                 Type[] genericArgs = t.GetGenericArguments();
100                 if (genericArgs != null && genericArgs.Length == 1)
101                 {
102                     return genericArgs[0].IsEnum;
103                 }
104             }
105
106             return false;
107         }
108
109         public const string NullString = "(null)";
110     }
111 }