mscorlib(4.0.0.0) API with additions
CodeIdentifiers.cs
1 using System.Collections;
3 
5 {
7  public class CodeIdentifiers
8  {
9  private Hashtable identifiers;
10 
11  private Hashtable reservedIdentifiers;
12 
13  private ArrayList list;
14 
15  private bool camelCase;
16 
20  public bool UseCamelCasing
21  {
22  get
23  {
24  return camelCase;
25  }
26  set
27  {
28  camelCase = value;
29  }
30  }
31 
33  public CodeIdentifiers()
34  : this(caseSensitive: true)
35  {
36  }
37 
41  public CodeIdentifiers(bool caseSensitive)
42  {
43  if (caseSensitive)
44  {
45  identifiers = new Hashtable();
46  reservedIdentifiers = new Hashtable();
47  }
48  else
49  {
50  IEqualityComparer equalityComparer = new CaseInsensitiveKeyComparer();
51  identifiers = new Hashtable(equalityComparer);
52  reservedIdentifiers = new Hashtable(equalityComparer);
53  }
54  list = new ArrayList();
55  }
56 
58  public void Clear()
59  {
60  identifiers.Clear();
61  list.Clear();
62  }
63 
67  public string MakeRightCase(string identifier)
68  {
69  if (camelCase)
70  {
71  return CodeIdentifier.MakeCamel(identifier);
72  }
73  return CodeIdentifier.MakePascal(identifier);
74  }
75 
79  public string MakeUnique(string identifier)
80  {
81  if (IsInUse(identifier))
82  {
83  int num = 1;
84  string text;
85  while (true)
86  {
87  text = identifier + num.ToString(CultureInfo.InvariantCulture);
88  if (!IsInUse(text))
89  {
90  break;
91  }
92  num++;
93  }
94  identifier = text;
95  }
96  if (identifier.Length > 511)
97  {
98  return MakeUnique("Item");
99  }
100  return identifier;
101  }
102 
105  public void AddReserved(string identifier)
106  {
107  reservedIdentifiers.Add(identifier, identifier);
108  }
109 
112  public void RemoveReserved(string identifier)
113  {
114  reservedIdentifiers.Remove(identifier);
115  }
116 
121  public string AddUnique(string identifier, object value)
122  {
123  identifier = MakeUnique(identifier);
124  Add(identifier, value);
125  return identifier;
126  }
127 
132  public bool IsInUse(string identifier)
133  {
134  if (!identifiers.Contains(identifier))
135  {
136  return reservedIdentifiers.Contains(identifier);
137  }
138  return true;
139  }
140 
144  public void Add(string identifier, object value)
145  {
146  identifiers.Add(identifier, value);
147  list.Add(value);
148  }
149 
152  public void Remove(string identifier)
153  {
154  list.Remove(identifiers[identifier]);
155  identifiers.Remove(identifier);
156  }
157 
161  public object ToArray(Type type)
162  {
163  Array array = Array.CreateInstance(type, list.Count);
164  list.CopyTo(array, 0);
165  return array;
166  }
167 
168  internal CodeIdentifiers Clone()
169  {
170  CodeIdentifiers codeIdentifiers = new CodeIdentifiers();
171  codeIdentifiers.identifiers = (Hashtable)identifiers.Clone();
172  codeIdentifiers.reservedIdentifiers = (Hashtable)reservedIdentifiers.Clone();
173  codeIdentifiers.list = (ArrayList)list.Clone();
174  codeIdentifiers.camelCase = camelCase;
175  return codeIdentifiers;
176  }
177  }
178 }
static CultureInfo InvariantCulture
Gets the T:System.Globalization.CultureInfo object that is culture-independent (invariant).
Definition: CultureInfo.cs:263
void Remove(string identifier)
Removes from the T:System.Xml.Serialization.CodeIdentifiers instance's scope the code entity or type ...
static string MakeCamel(string identifier)
Produces a camel-case string from an input string.
virtual void Add(object key, object value)
Adds an element with the specified key and value into the T:System.Collections.Hashtable.
Definition: Hashtable.cs:916
string AddUnique(string identifier, object value)
Adds a named code entity or type mapping to the T:System.Xml.Serialization.CodeIdentifiers instance's...
bool IsInUse(string identifier)
Determines whether a specified name is already being used within the T:System.Xml....
Definition: __Canon.cs:3
virtual object Clone()
Creates a shallow copy of the T:System.Collections.Hashtable.
Definition: Hashtable.cs:946
void Clear()
Removes all code entities or type mappings, including their names, from the T:System....
bool UseCamelCasing
Gets or sets a value that indicates whether the instance uses camel case.
void AddReserved(string identifier)
Prevents a specified name from being used within the T:System.Xml.Serialization.CodeIdentifiers insta...
CodeIdentifiers()
Initializes a new instance of the T:System.Xml.Serialization.CodeIdentifiers class.
void RemoveReserved(string identifier)
Removes the input name from the T:System.Xml.Serialization.CodeIdentifiers instance's reserved names.
Represents a collection of key/value pairs that are organized based on the hash code of the key....
Definition: Hashtable.cs:17
Defines methods to support the comparison of objects for equality.
string MakeUnique(string identifier)
Determines whether the input name conflicts with another name within the T:System....
Provides methods for creating, manipulating, searching, and sorting arrays, thereby serving as the ba...
Definition: Array.cs:17
Represents type declarations: class types, interface types, array types, value types,...
Definition: Type.cs:18
string MakeRightCase(string identifier)
Ensures that the input is of the correct case by modifying the name according to the value of the P:S...
void Add(string identifier, object value)
Adds a named code entity or type mapping to the T:System.Xml.Serialization.CodeIdentifiers instance's...
virtual bool Contains(object key)
Determines whether the T:System.Collections.Hashtable contains a specific key.
Definition: Hashtable.cs:972
static string MakePascal(string identifier)
Produces a Pascal-case string from an input string.
object ToArray(Type type)
Returns an array of the code entities or type mappings within the T:System.Xml.Serialization....
virtual void Remove(object key)
Removes the element with the specified key from the T:System.Collections.Hashtable.
Definition: Hashtable.cs:1349
virtual void Clear()
Removes all elements from the T:System.Collections.Hashtable.
Definition: Hashtable.cs:924
Provides information about a specific culture (called a locale for unmanaged code development)....
Definition: CultureInfo.cs:16
Maintains a group of names for related code entities or type mappings that are generated by the ....
CodeIdentifiers(bool caseSensitive)
Initializes a new instance of the T:System.Xml.Serialization.CodeIdentifiers class and determines whe...
Provides static methods to convert input text into names for code entities.
static unsafe Array CreateInstance(Type elementType, int length)
Creates a one-dimensional T:System.Array of the specified T:System.Type and length,...
Definition: Array.cs:972
Implements the T:System.Collections.IList interface using an array whose size is dynamically increase...
Definition: ArrayList.cs:14