231a93fb8ace6411a510d3a1f455a09761aba3bf
[mono.git] / mcs / class / referencesource / System.Activities.Presentation / Microsoft.Tools.Common / Microsoft / Activities / Presentation / TypeNameHelper.cs
1 //----------------------------------------------------------------
2 // <copyright company="Microsoft Corporation">
3 //     Copyright (c) Microsoft Corporation.  All rights reserved.
4 // </copyright>
5 //----------------------------------------------------------------
6
7 namespace Microsoft.Activities.Presentation
8 {
9     using System;
10     using System.Globalization;
11     using System.Text;
12     using System.Text.RegularExpressions;
13
14     internal static class TypeNameHelper
15     {
16         // note: does not work for nested type when fullName is true
17         // eg. Namespace.DeclaringType.NestedType<T> will be displayed
18         // as  Namespace.DeclaringType+NestedType<T>
19         public static string GetDisplayName(Type type, bool fullName)
20         {
21             if (type == null)
22             {
23                 return string.Empty;
24             }
25
26             if (type.IsGenericParameter)
27             {
28                 return type.Name;
29             }
30
31             if (!type.IsGenericType && !type.IsArray)
32             {
33                 if (fullName)
34                 {
35                     return type.FullName;
36                 }
37                 else
38                 {
39                     return type.Name;
40                 }
41             }
42
43             // replace `2 with <Type1, Type2>
44             Regex regex = new Regex("`[0-9]+");
45             GenericsMatchEvaluator evaluator = new GenericsMatchEvaluator(type.GetGenericArguments(), fullName);
46
47             // Remove [[fullName1, ..., fullNameX]]
48             string name;
49             if (fullName)
50             {
51                 name = type.FullName;
52             }
53             else
54             {
55                 name = type.Name;
56             }
57
58             int start = name.IndexOf("[[", StringComparison.Ordinal);
59             int end = name.LastIndexOf("]]", StringComparison.Ordinal);
60             if (start > 0 && end > 0)
61             {
62                 name = name.Substring(0, start) + name.Substring(end + 2);
63             }
64
65             return regex.Replace(name, evaluator.Evaluate);
66         }
67
68         private class GenericsMatchEvaluator
69         {
70             private Type[] generics = null;
71             private int index;
72             private bool fullName;
73
74             public GenericsMatchEvaluator(Type[] generics, bool fullName)
75             {
76                 this.generics = generics;
77                 this.index = 0;
78                 this.fullName = fullName;
79             }
80
81             public string Evaluate(Match match)
82             {
83                 int numberOfParameters = int.Parse(match.Value.Substring(1), CultureInfo.InvariantCulture);
84
85                 StringBuilder sb = new StringBuilder();
86
87                 // matched "`N" is replaced by "<Type1, ..., TypeN>"
88                 sb.Append("<");
89
90                 for (int i = 0; i < numberOfParameters; i++)
91                 {
92                     if (i > 0)
93                     {
94                         sb.Append(", ");
95                     }
96
97                     sb.Append(TypeNameHelper.GetDisplayName(this.generics[this.index++], this.fullName));
98                 }
99
100                 sb.Append(">");
101
102                 return sb.ToString();
103             }
104         }
105     }
106 }