CollectionCodeDomSerializer.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. //
  2. // System.ComponentModel.Design.Serialization.CollectionCodeDomSerializer
  3. //
  4. // Authors:
  5. // Ivan N. Zlatev (contact i-nZ.net)
  6. //
  7. // (C) 2007 Ivan N. Zlatev
  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. #if NET_2_0
  29. using System;
  30. using System.Collections;
  31. using System.Reflection;
  32. using System.ComponentModel;
  33. using System.ComponentModel.Design;
  34. using System.CodeDom;
  35. namespace System.ComponentModel.Design.Serialization
  36. {
  37. public class CollectionCodeDomSerializer : CodeDomSerializer
  38. {
  39. public CollectionCodeDomSerializer ()
  40. {
  41. }
  42. // FIXME: What is this supposed to do?
  43. protected bool MethodSupportsSerialization (MethodInfo method)
  44. {
  45. return true;
  46. }
  47. public override object Serialize (IDesignerSerializationManager manager, object value)
  48. {
  49. if (value == null)
  50. throw new ArgumentNullException ("value");
  51. if (manager == null)
  52. throw new ArgumentNullException ("manager");
  53. ICollection originalCollection = value as ICollection;
  54. if (originalCollection == null)
  55. throw new ArgumentException ("originalCollection is not an ICollection");
  56. CodeExpression targetExpression = null;
  57. ExpressionContext exprContext = manager.Context[typeof (ExpressionContext)] as ExpressionContext;
  58. RootContext root = manager.Context[typeof (RootContext)] as RootContext;
  59. if (exprContext != null && exprContext.PresetValue == value)
  60. targetExpression = exprContext.Expression;
  61. else if (root != null)
  62. targetExpression = root.Expression;
  63. ArrayList valuesToSerialize = new ArrayList ();
  64. foreach (object o in originalCollection)
  65. valuesToSerialize.Add (o);
  66. return this.SerializeCollection (manager, targetExpression, value.GetType (), originalCollection, valuesToSerialize);
  67. }
  68. protected virtual object SerializeCollection (IDesignerSerializationManager manager, CodeExpression targetExpression,
  69. Type targetType, ICollection originalCollection, ICollection valuesToSerialize)
  70. {
  71. if (valuesToSerialize == null)
  72. throw new ArgumentNullException ("valuesToSerialize");
  73. if (originalCollection == null)
  74. throw new ArgumentNullException ("originalCollection");
  75. if (targetType == null)
  76. throw new ArgumentNullException ("targetType");
  77. if (manager == null)
  78. throw new ArgumentNullException ("manager");
  79. if (valuesToSerialize.Count == 0)
  80. return null;
  81. MethodInfo method = null;
  82. try {
  83. object sampleParam = null;
  84. IEnumerator e = valuesToSerialize.GetEnumerator ();
  85. e.MoveNext ();
  86. sampleParam = e.Current;
  87. // try to find a method matching the type of the sample parameter.
  88. // Assuming objects in the collection are from the same base type
  89. method = GetExactMethod (targetType, "Add", new object [] { sampleParam });
  90. } catch {
  91. Console.WriteLine ("SerializeCollection: No compatible Add method found in " + targetType);
  92. }
  93. if (method == null)
  94. return null;
  95. CodeStatementCollection statements = new CodeStatementCollection ();
  96. foreach (object value in valuesToSerialize) {
  97. CodeMethodInvokeExpression methodInvoke = new CodeMethodInvokeExpression ();
  98. methodInvoke.Method = new CodeMethodReferenceExpression (targetExpression, "Add");
  99. manager.Context.Push (new ExpressionContext (methodInvoke, methodInvoke.GetType (), null, originalCollection));
  100. CodeExpression expression = base.SerializeToExpression (manager, value);
  101. if (expression == null) {
  102. Console.WriteLine ("SerializeCollection: Unable to serialize " + value);
  103. methodInvoke = null;
  104. } else {
  105. methodInvoke.Parameters.AddRange (new CodeExpression[] { expression });
  106. }
  107. manager.Context.Pop ();
  108. if (methodInvoke != null)
  109. statements.Add (methodInvoke);
  110. }
  111. return statements;
  112. }
  113. // Searches for a method on type that matches argument types
  114. //
  115. private MethodInfo GetExactMethod (Type type, string methodName, ICollection argsCollection)
  116. {
  117. object[] arguments = null;
  118. Type[] types = new Type[0];
  119. if (argsCollection != null) {
  120. arguments = new object[argsCollection.Count];
  121. types = new Type[argsCollection.Count];
  122. argsCollection.CopyTo (arguments, 0);
  123. for (int i=0; i < arguments.Length; i++) {
  124. if (arguments[i] == null)
  125. types[i] = null;
  126. else
  127. types[i] = arguments[i].GetType ();
  128. }
  129. }
  130. return type.GetMethod (methodName, types);
  131. }
  132. }
  133. }
  134. #endif