mscorlib(4.0.0.0) API with additions
XmlQualifiedName.cs
1 using Microsoft.Win32;
2 using System.Reflection;
3 using System.Security;
5 
6 namespace System.Xml
7 {
10  [global::__DynamicallyInvokable]
11  public class XmlQualifiedName
12  {
13  private delegate int HashCodeOfStringDelegate(string s, int sLen, long additionalEntropy);
14 
15  private static HashCodeOfStringDelegate hashCodeDelegate = null;
16 
17  private string name;
18 
19  private string ns;
20 
21  [NonSerialized]
22  private int hash;
23 
25  [global::__DynamicallyInvokable]
26  public static readonly XmlQualifiedName Empty = new XmlQualifiedName(string.Empty);
27 
30  [global::__DynamicallyInvokable]
31  public string Namespace
32  {
33  [global::__DynamicallyInvokable]
34  get
35  {
36  return ns;
37  }
38  }
39 
42  [global::__DynamicallyInvokable]
43  public string Name
44  {
45  [global::__DynamicallyInvokable]
46  get
47  {
48  return name;
49  }
50  }
51 
55  [global::__DynamicallyInvokable]
56  public bool IsEmpty
57  {
58  [global::__DynamicallyInvokable]
59  get
60  {
61  if (Name.Length == 0)
62  {
63  return Namespace.Length == 0;
64  }
65  return false;
66  }
67  }
68 
70  [global::__DynamicallyInvokable]
72  : this(string.Empty, string.Empty)
73  {
74  }
75 
78  [global::__DynamicallyInvokable]
79  public XmlQualifiedName(string name)
80  : this(name, string.Empty)
81  {
82  }
83 
87  [global::__DynamicallyInvokable]
88  public XmlQualifiedName(string name, string ns)
89  {
90  this.ns = ((ns == null) ? string.Empty : ns);
91  this.name = ((name == null) ? string.Empty : name);
92  }
93 
96  [global::__DynamicallyInvokable]
97  public override int GetHashCode()
98  {
99  if (hash == 0)
100  {
101  if (hashCodeDelegate == null)
102  {
103  hashCodeDelegate = GetHashCodeDelegate();
104  }
105  hash = hashCodeDelegate(Name, Name.Length, 0L);
106  }
107  return hash;
108  }
109 
112  [global::__DynamicallyInvokable]
113  public override string ToString()
114  {
115  if (Namespace.Length != 0)
116  {
117  return Namespace + ":" + Name;
118  }
119  return Name;
120  }
121 
126  [global::__DynamicallyInvokable]
127  public override bool Equals(object other)
128  {
129  if (this == other)
130  {
131  return true;
132  }
133  XmlQualifiedName xmlQualifiedName = other as XmlQualifiedName;
134  if (xmlQualifiedName != null)
135  {
136  if (Name == xmlQualifiedName.Name)
137  {
138  return Namespace == xmlQualifiedName.Namespace;
139  }
140  return false;
141  }
142  return false;
143  }
144 
150  [global::__DynamicallyInvokable]
152  {
153  if ((object)a == b)
154  {
155  return true;
156  }
157  if ((object)a == null || (object)b == null)
158  {
159  return false;
160  }
161  if (a.Name == b.Name)
162  {
163  return a.Namespace == b.Namespace;
164  }
165  return false;
166  }
167 
173  [global::__DynamicallyInvokable]
175  {
176  return !(a == b);
177  }
178 
183  [global::__DynamicallyInvokable]
184  public static string ToString(string name, string ns)
185  {
186  if (ns != null && ns.Length != 0)
187  {
188  return ns + ":" + name;
189  }
190  return name;
191  }
192 
193  [SecuritySafeCritical]
194  [ReflectionPermission(SecurityAction.Assert, Unrestricted = true)]
195  private static HashCodeOfStringDelegate GetHashCodeDelegate()
196  {
197  if (!IsRandomizedHashingDisabled())
198  {
199  MethodInfo method = typeof(string).GetMethod("InternalMarvin32HashString", BindingFlags.Static | BindingFlags.NonPublic);
200  if (method != null)
201  {
202  return (HashCodeOfStringDelegate)Delegate.CreateDelegate(typeof(HashCodeOfStringDelegate), method);
203  }
204  }
205  return GetHashCodeOfString;
206  }
207 
208  [SecuritySafeCritical]
209  [RegistryPermission(SecurityAction.Assert, Unrestricted = true)]
210  private static bool IsRandomizedHashingDisabled()
211  {
212  bool value = false;
213  if (!ReadBoolFromXmlRegistrySettings(Registry.CurrentUser, "DisableRandomizedHashingOnXmlQualifiedName", ref value))
214  {
215  ReadBoolFromXmlRegistrySettings(Registry.LocalMachine, "DisableRandomizedHashingOnXmlQualifiedName", ref value);
216  }
217  return value;
218  }
219 
220  [SecurityCritical]
221  private static bool ReadBoolFromXmlRegistrySettings(RegistryKey hive, string regValueName, ref bool value)
222  {
223  try
224  {
225  using (RegistryKey registryKey = hive.OpenSubKey("SOFTWARE\\Microsoft\\.NETFramework\\XML", writable: false))
226  {
227  if (registryKey != null && registryKey.GetValueKind(regValueName) == RegistryValueKind.DWord)
228  {
229  value = ((int)registryKey.GetValue(regValueName) == 1);
230  return true;
231  }
232  }
233  }
234  catch
235  {
236  }
237  return false;
238  }
239 
240  private static int GetHashCodeOfString(string s, int length, long additionalEntropy)
241  {
242  return s.GetHashCode();
243  }
244 
245  internal void Init(string name, string ns)
246  {
247  this.name = name;
248  this.ns = ns;
249  hash = 0;
250  }
251 
252  internal void SetNamespace(string ns)
253  {
254  this.ns = ns;
255  }
256 
257  internal void Verify()
258  {
259  XmlConvert.VerifyNCName(name);
260  if (ns.Length != 0)
261  {
262  XmlConvert.ToUri(ns);
263  }
264  }
265 
266  internal void Atomize(XmlNameTable nameTable)
267  {
268  name = nameTable.Add(name);
269  ns = nameTable.Add(ns);
270  }
271 
272  internal static XmlQualifiedName Parse(string s, IXmlNamespaceResolver nsmgr, out string prefix)
273  {
274  ValidateNames.ParseQNameThrow(s, out prefix, out string localName);
275  string text = nsmgr.LookupNamespace(prefix);
276  if (text == null)
277  {
278  if (prefix.Length != 0)
279  {
280  throw new XmlException("Xml_UnknownNs", prefix);
281  }
282  text = string.Empty;
283  }
284  return new XmlQualifiedName(localName, text);
285  }
286 
287  internal XmlQualifiedName Clone()
288  {
289  return (XmlQualifiedName)MemberwiseClone();
290  }
291 
292  internal static int Compare(XmlQualifiedName a, XmlQualifiedName b)
293  {
294  if (null == a)
295  {
296  if (!(null == b))
297  {
298  return -1;
299  }
300  return 0;
301  }
302  if (null == b)
303  {
304  return 1;
305  }
306  int num = string.CompareOrdinal(a.Namespace, b.Namespace);
307  if (num == 0)
308  {
309  num = string.CompareOrdinal(a.Name, b.Name);
310  }
311  return num;
312  }
313  }
314 }
static string ToString(string name, string ns)
Returns the string value of the T:System.Xml.XmlQualifiedName.
Discovers the attributes of a method and provides access to method metadata.
Definition: MethodInfo.cs:13
Controls the ability to access registry variables. This class cannot be inherited.
BindingFlags
Specifies flags that control binding and the way in which the search for members and types is conduct...
Definition: BindingFlags.cs:10
Definition: __Canon.cs:3
static bool operator !=(XmlQualifiedName a, XmlQualifiedName b)
Compares two T:System.Xml.XmlQualifiedName objects.
string Namespace
Gets a string representation of the namespace of the T:System.Xml.XmlQualifiedName.
override bool Equals(object other)
Determines whether the specified T:System.Xml.XmlQualifiedName object is equal to the current T:Syste...
XmlQualifiedName()
Initializes a new instance of the T:System.Xml.XmlQualifiedName class.
SecurityAction
Specifies the security actions that can be performed using declarative security.
string Name
Gets a string representation of the qualified name of the T:System.Xml.XmlQualifiedName.
XmlQualifiedName(string name)
Initializes a new instance of the T:System.Xml.XmlQualifiedName class with the specified name.
static bool operator==(XmlQualifiedName a, XmlQualifiedName b)
Compares two T:System.Xml.XmlQualifiedName objects.
Represents a delegate, which is a data structure that refers to a static method or to a class instanc...
Definition: Delegate.cs:15
XmlQualifiedName(string name, string ns)
Initializes a new instance of the T:System.Xml.XmlQualifiedName class with the specified name and nam...
Represents an XML qualified name.
static readonly XmlQualifiedName Empty
Provides an empty T:System.Xml.XmlQualifiedName.
override string ToString()
Returns the string value of the T:System.Xml.XmlQualifiedName.
Specifies that the class can be serialized.
bool IsEmpty
Gets a value indicating whether the T:System.Xml.XmlQualifiedName is empty.
Controls access to non-public types and members through the N:System.Reflection APIs....
static Delegate CreateDelegate(Type type, object target, string method)
Creates a delegate of the specified type that represents the specified instance method to invoke on t...
Definition: Delegate.cs:368
override int GetHashCode()
Returns the hash code for the T:System.Xml.XmlQualifiedName.