ReflectionHelper.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. //
  2. // System.Xml.Serialization.ReflectionHelper
  3. //
  4. // Author:
  5. // Lluis Sanchez Gual ([email protected])
  6. //
  7. // Copyright (C) 2003 Ximian, Inc.
  8. //
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. using System.Reflection;
  30. using System.Collections;
  31. namespace System.Xml.Serialization
  32. {
  33. internal class ReflectionHelper
  34. {
  35. Hashtable _clrTypes = new Hashtable ();
  36. Hashtable _schemaTypes = new Hashtable ();
  37. public void RegisterSchemaType (XmlTypeMapping map, string xmlType, string ns)
  38. {
  39. string mapKey = xmlType + "/" + ns;
  40. if (!_schemaTypes.ContainsKey (mapKey))
  41. _schemaTypes.Add (mapKey, map);
  42. }
  43. public XmlTypeMapping GetRegisteredSchemaType (string xmlType, string ns)
  44. {
  45. string mapKey = xmlType + "/" + ns;
  46. return _schemaTypes[mapKey] as XmlTypeMapping;
  47. }
  48. public void RegisterClrType (XmlTypeMapping map, Type type, string ns)
  49. {
  50. if (type == typeof(object)) ns = "";
  51. string mapKey = type.FullName + "/" + ns;
  52. if (!_clrTypes.ContainsKey (mapKey))
  53. _clrTypes.Add (mapKey, map);
  54. }
  55. public XmlTypeMapping GetRegisteredClrType (Type type, string ns)
  56. {
  57. if (type == typeof(object)) ns = "";
  58. string mapKey = type.FullName + "/" + ns;
  59. return _clrTypes[mapKey] as XmlTypeMapping;
  60. }
  61. public Exception CreateError (XmlTypeMapping map, string message)
  62. {
  63. return new InvalidOperationException ("There was an error reflecting '" + map.TypeFullName + "': " + message);
  64. }
  65. static readonly ParameterModifier [] empty_modifiers = new ParameterModifier [0];
  66. public static void CheckSerializableType (Type type, bool allowPrivateConstructors)
  67. {
  68. if (type.IsArray) return;
  69. #if NET_2_0
  70. if (!allowPrivateConstructors && type.GetConstructor (BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance, null, Type.EmptyTypes, empty_modifiers) == null && !type.IsAbstract && !type.IsValueType)
  71. #else
  72. if (!allowPrivateConstructors && type.GetConstructor (Type.EmptyTypes) == null && !type.IsAbstract && !type.IsValueType)
  73. #endif
  74. throw new InvalidOperationException (type.FullName + " cannot be serialized because it does not have a default public constructor");
  75. if (type.IsInterface && !TypeTranslator.GetTypeData (type).IsListType)
  76. throw new InvalidOperationException (type.FullName + " cannot be serialized because it is an interface");
  77. Type t = type;
  78. Type oldt = null;
  79. do {
  80. if (!t.IsPublic && !t.IsNestedPublic)
  81. throw new InvalidOperationException (type.FullName + " is inaccessible due to its protection level. Only public types can be processed");
  82. oldt = t;
  83. t = t.DeclaringType;
  84. }
  85. while (t != null && t != oldt);
  86. }
  87. public static string BuildMapKey (Type type)
  88. {
  89. return type.FullName + "::";
  90. }
  91. public static string BuildMapKey (MethodInfo method, string tag)
  92. {
  93. string res = method.DeclaringType.FullName + ":" + method.ReturnType.FullName + " " + method.Name + "(";
  94. ParameterInfo[] pars = method.GetParameters ();
  95. for (int n=0; n<pars.Length; n++)
  96. {
  97. if (n > 0) res += ", ";
  98. res += pars[n].ParameterType.FullName;
  99. }
  100. res += ")";
  101. if (tag != null)
  102. res += ":" + tag;
  103. return res;
  104. }
  105. }
  106. }