ReflectionHelper.cs 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. //
  2. // System.Xml.Serialization.ReflectionHelper
  3. //
  4. // Author:
  5. // Lluis Sanchez Gual ([email protected])
  6. //
  7. // Copyright (C) 2003 Ximian, Inc.
  8. //
  9. using System.Reflection;
  10. using System.Collections;
  11. namespace System.Xml.Serialization
  12. {
  13. internal class ReflectionHelper
  14. {
  15. Hashtable _clrTypes = new Hashtable ();
  16. Hashtable _schemaTypes = new Hashtable ();
  17. public void RegisterSchemaType (XmlTypeMapping map, string xmlType, string ns)
  18. {
  19. string mapKey = xmlType + "/" + ns;
  20. if (!_schemaTypes.ContainsKey (xmlType))
  21. _schemaTypes.Add (mapKey, map);
  22. }
  23. public XmlTypeMapping GetRegisteredSchemaType (string xmlType, string ns)
  24. {
  25. string mapKey = xmlType + "/" + ns;
  26. return _schemaTypes[mapKey] as XmlTypeMapping;
  27. }
  28. public void RegisterClrType (XmlTypeMapping map, Type type, string ns)
  29. {
  30. if (type == typeof(object)) ns = "";
  31. string mapKey = type.FullName + "/" + ns;
  32. if (!_clrTypes.ContainsKey (mapKey))
  33. _clrTypes.Add (mapKey, map);
  34. }
  35. public XmlTypeMapping GetRegisteredClrType (Type type, string ns)
  36. {
  37. if (type == typeof(object)) ns = "";
  38. string mapKey = type.FullName + "/" + ns;
  39. return _clrTypes[mapKey] as XmlTypeMapping;
  40. }
  41. public Exception CreateError (XmlTypeMapping map, string message)
  42. {
  43. return new InvalidOperationException ("There was an error reflecting '" + map.TypeFullName + "': " + message);
  44. }
  45. public static void CheckSerializableType (Type type)
  46. {
  47. if (type.IsArray) return;
  48. if (type.GetConstructor (Type.EmptyTypes) == null && !type.IsAbstract && !type.IsValueType)
  49. throw new InvalidOperationException (type.FullName + " cannot be serialized because it does not have a default public constructor");
  50. if (type.IsInterface)
  51. throw new InvalidOperationException (type.FullName + " cannot be serialized because it is an interface");
  52. if (type.IsNotPublic)
  53. throw new InvalidOperationException (type.FullName + " is inaccessible due to its protection level. Only public types can be processed");
  54. }
  55. }
  56. }