ReflectionSerializationHolder.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. // ReflectionSerializationHolder.cs.cs
  2. //
  3. // Author:
  4. // Patrik Torstensson
  5. //
  6. // (C) 2003 Patrik Torstensson
  7. //
  8. // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
  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;
  30. using System.Runtime.Serialization;
  31. namespace System.Reflection
  32. {
  33. [Serializable]
  34. internal class ReflectionSerializationHolder : IObjectReference, ISerializable
  35. {
  36. string _memberName;
  37. string _memberSignature;
  38. MemberTypes _memberType;
  39. Type _reflectedType;
  40. ReflectionSerializationHolder(SerializationInfo info, StreamingContext ctx)
  41. {
  42. string assemblyName;
  43. string typeName;
  44. assemblyName = info.GetString("AssemblyName");
  45. typeName = info.GetString("ClassName");
  46. _memberName = info.GetString("Name");
  47. _memberSignature = info.GetString("Signature");
  48. _memberType = (MemberTypes) info.GetInt32("MemberType");
  49. // Load type
  50. Assembly asm = Assembly.Load(assemblyName);
  51. _reflectedType = asm.GetType(typeName, true, true);
  52. }
  53. public static void Serialize(SerializationInfo info, String name, Type klass, String signature, MemberTypes type)
  54. {
  55. info.SetType( typeof(ReflectionSerializationHolder));
  56. info.AddValue("AssemblyName", klass.Module.Assembly.FullName, typeof(String));
  57. info.AddValue("ClassName", klass.FullName, typeof(String));
  58. info.AddValue("Name", name, typeof(String));
  59. info.AddValue("Signature", signature, typeof(String));
  60. info.AddValue("MemberType",(int)type);
  61. }
  62. public void GetObjectData(SerializationInfo info, StreamingContext context)
  63. {
  64. throw new NotSupportedException();
  65. }
  66. public object GetRealObject(StreamingContext context)
  67. {
  68. switch (_memberType)
  69. {
  70. case MemberTypes.Constructor:
  71. ConstructorInfo [] ctors;
  72. ctors = _reflectedType.GetConstructors (BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static);
  73. for (int i = 0; i < ctors.Length; i++)
  74. if ( ctors[i].ToString().Equals(_memberSignature))
  75. return ctors[i];
  76. throw new SerializationException("Failed to find serialized constructor");
  77. case MemberTypes.Method:
  78. MethodInfo [] methods;
  79. methods = _reflectedType.GetMethods(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static);
  80. for (int i = 0; i < methods.Length; i++)
  81. if ((methods[i]).ToString().Equals(_memberSignature))
  82. return methods[i];
  83. throw new SerializationException("Failed to find serialized method");
  84. default:
  85. throw new SerializationException("Failed to get object for member type " + _memberType.ToString());
  86. }
  87. }
  88. }
  89. }