ReflectionSerializationHolder.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. // ReflectionSerializationHolder.cs
  2. //
  3. // Note: this class was superseded by MemberInfoSerializationHolder but
  4. // it's still included in the build to provide backwards compatibility.
  5. //
  6. // Author:
  7. // Patrik Torstensson
  8. //
  9. // (C) 2003 Patrik Torstensson
  10. //
  11. // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
  12. //
  13. // Permission is hereby granted, free of charge, to any person obtaining
  14. // a copy of this software and associated documentation files (the
  15. // "Software"), to deal in the Software without restriction, including
  16. // without limitation the rights to use, copy, modify, merge, publish,
  17. // distribute, sublicense, and/or sell copies of the Software, and to
  18. // permit persons to whom the Software is furnished to do so, subject to
  19. // the following conditions:
  20. //
  21. // The above copyright notice and this permission notice shall be
  22. // included in all copies or substantial portions of the Software.
  23. //
  24. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  25. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  26. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  27. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  28. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  29. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  30. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  31. //
  32. using System;
  33. using System.Runtime.Serialization;
  34. namespace System.Reflection
  35. {
  36. [Serializable]
  37. internal class ReflectionSerializationHolder : IObjectReference, ISerializable
  38. {
  39. string _memberName;
  40. string _memberSignature;
  41. MemberTypes _memberType;
  42. Type _reflectedType;
  43. ReflectionSerializationHolder(SerializationInfo info, StreamingContext ctx)
  44. {
  45. string assemblyName;
  46. string typeName;
  47. assemblyName = info.GetString("AssemblyName");
  48. typeName = info.GetString("ClassName");
  49. _memberName = info.GetString("Name");
  50. _memberSignature = info.GetString("Signature");
  51. _memberType = (MemberTypes) info.GetInt32("MemberType");
  52. // Load type
  53. Assembly asm = Assembly.Load(assemblyName);
  54. _reflectedType = asm.GetType(typeName, true, true);
  55. }
  56. public static void Serialize(SerializationInfo info, String name, Type klass, String signature, MemberTypes type)
  57. {
  58. info.SetType( typeof(ReflectionSerializationHolder));
  59. info.AddValue("AssemblyName", klass.Module.Assembly.FullName, typeof(String));
  60. info.AddValue("ClassName", klass.FullName, typeof(String));
  61. info.AddValue("Name", name, typeof(String));
  62. info.AddValue("Signature", signature, typeof(String));
  63. info.AddValue("MemberType",(int)type);
  64. }
  65. public void GetObjectData(SerializationInfo info, StreamingContext context)
  66. {
  67. throw new NotSupportedException();
  68. }
  69. public object GetRealObject(StreamingContext context)
  70. {
  71. switch (_memberType)
  72. {
  73. case MemberTypes.Constructor:
  74. ConstructorInfo [] ctors;
  75. ctors = _reflectedType.GetConstructors (BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static);
  76. for (int i = 0; i < ctors.Length; i++)
  77. if ( ctors[i].ToString().Equals(_memberSignature))
  78. return ctors[i];
  79. throw new SerializationException("Failed to find serialized constructor");
  80. case MemberTypes.Method:
  81. MethodInfo [] methods;
  82. methods = _reflectedType.GetMethods(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static);
  83. for (int i = 0; i < methods.Length; i++)
  84. if ((methods[i]).ToString().Equals(_memberSignature))
  85. return methods[i];
  86. throw new SerializationException("Failed to find serialized method");
  87. default:
  88. throw new SerializationException("Failed to get object for member type " + _memberType.ToString());
  89. }
  90. }
  91. }
  92. }