PageRenderTime 52ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/mcs/class/corlib/System.Runtime.Serialization/SerializationCallbacks.cs

https://bitbucket.org/danipen/mono
C# | 160 lines | 100 code | 29 blank | 31 comment | 15 complexity | 94f40674a274edc5b590c06bfd9e995a MD5 | raw file
Possible License(s): Unlicense, Apache-2.0, LGPL-2.0, MPL-2.0-no-copyleft-exception, CC-BY-SA-3.0, GPL-2.0
  1. //
  2. // System.Runtime.Serialization.SerializationCallbacks.cs
  3. //
  4. // Author:
  5. // Robert Jordan (robertj@gmx.net)
  6. //
  7. // Copyright (C) 2006 Novell, Inc (http://www.novell.com)
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. using System;
  29. using System.Collections;
  30. using System.Reflection;
  31. namespace System.Runtime.Serialization {
  32. internal sealed class SerializationCallbacks
  33. {
  34. public delegate void CallbackHandler (StreamingContext context);
  35. readonly ArrayList onSerializingList;
  36. readonly ArrayList onSerializedList;
  37. readonly ArrayList onDeserializingList;
  38. readonly ArrayList onDeserializedList;
  39. public bool HasSerializingCallbacks {
  40. get {return onSerializingList != null;}
  41. }
  42. public bool HasSerializedCallbacks {
  43. get {return onSerializedList != null;}
  44. }
  45. public bool HasDeserializingCallbacks {
  46. get {return onDeserializingList != null;}
  47. }
  48. public bool HasDeserializedCallbacks {
  49. get {return onDeserializedList != null;}
  50. }
  51. public SerializationCallbacks (Type type)
  52. {
  53. onSerializingList = GetMethodsByAttribute (type, typeof (OnSerializingAttribute));
  54. onSerializedList = GetMethodsByAttribute (type, typeof (OnSerializedAttribute));
  55. onDeserializingList = GetMethodsByAttribute (type, typeof (OnDeserializingAttribute));
  56. onDeserializedList = GetMethodsByAttribute (type, typeof (OnDeserializedAttribute));
  57. }
  58. const BindingFlags DefaultBindingFlags = BindingFlags.Public | BindingFlags.NonPublic |
  59. BindingFlags.Instance | BindingFlags.DeclaredOnly;
  60. static ArrayList GetMethodsByAttribute (Type type, Type attr)
  61. {
  62. ArrayList list = new ArrayList ();
  63. Type t = type;
  64. while (t != typeof (object)) {
  65. int count = 0;
  66. foreach (MethodInfo mi in t.GetMethods (DefaultBindingFlags)) {
  67. if (mi.IsDefined (attr, false)) {
  68. list.Add (mi);
  69. count++;
  70. }
  71. }
  72. // FIXME: MS.NET is checking for this with the verifier at assembly load time.
  73. if (count > 1)
  74. throw new TypeLoadException (
  75. String.Format ("Type '{0}' has more than one method with the following attribute: '{1}'.", type.AssemblyQualifiedName, attr.FullName));
  76. t = t.BaseType;
  77. }
  78. // optimize memory usage
  79. return list.Count == 0 ? null : list;
  80. }
  81. static void Invoke (ArrayList list, object target, StreamingContext context)
  82. {
  83. if (list == null)
  84. return;
  85. CallbackHandler handler = null;
  86. // construct a delegate from the specified list
  87. foreach (MethodInfo mi in list) {
  88. handler = (CallbackHandler)
  89. Delegate.Combine (
  90. Delegate.CreateDelegate (typeof (CallbackHandler), target, mi),
  91. handler);
  92. }
  93. handler (context);
  94. }
  95. public void RaiseOnSerializing (object target, StreamingContext contex)
  96. {
  97. Invoke (onSerializingList, target, contex);
  98. }
  99. public void RaiseOnSerialized (object target, StreamingContext contex)
  100. {
  101. Invoke (onSerializedList, target, contex);
  102. }
  103. public void RaiseOnDeserializing (object target, StreamingContext contex)
  104. {
  105. Invoke (onDeserializingList, target, contex);
  106. }
  107. public void RaiseOnDeserialized (object target, StreamingContext contex)
  108. {
  109. Invoke (onDeserializedList, target, contex);
  110. }
  111. static Hashtable cache = new Hashtable ();
  112. static object cache_lock = new object ();
  113. public static SerializationCallbacks GetSerializationCallbacks (Type t)
  114. {
  115. SerializationCallbacks sc = (SerializationCallbacks) cache [t];
  116. if (sc != null)
  117. return sc;
  118. // Slow path, new entry, we need to copy
  119. lock (cache_lock){
  120. sc = (SerializationCallbacks) cache [t];
  121. if (sc == null) {
  122. Hashtable copy = (Hashtable) cache.Clone ();
  123. sc = new SerializationCallbacks (t);
  124. copy [t] = sc;
  125. cache = copy;
  126. }
  127. return sc;
  128. }
  129. }
  130. }
  131. }