MemberInfoSerializationHolder.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. // MemberInfoSerializationHolder.cs.cs
  2. //
  3. // Author:
  4. // Patrik Torstensson
  5. // Robert Jordan <[email protected]>
  6. //
  7. // (C) 2003 Patrik Torstensson
  8. //
  9. // Copyright (C) 2004-2007 Novell, Inc (http://www.novell.com)
  10. //
  11. // Permission is hereby granted, free of charge, to any person obtaining
  12. // a copy of this software and associated documentation files (the
  13. // "Software"), to deal in the Software without restriction, including
  14. // without limitation the rights to use, copy, modify, merge, publish,
  15. // distribute, sublicense, and/or sell copies of the Software, and to
  16. // permit persons to whom the Software is furnished to do so, subject to
  17. // the following conditions:
  18. //
  19. // The above copyright notice and this permission notice shall be
  20. // included in all copies or substantial portions of the Software.
  21. //
  22. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  23. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  24. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  25. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  26. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  27. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  28. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  29. //
  30. using System;
  31. using System.Runtime.Serialization;
  32. namespace System.Reflection
  33. {
  34. [Serializable]
  35. internal class MemberInfoSerializationHolder : IObjectReference, ISerializable
  36. {
  37. const BindingFlags DefaultBinding = BindingFlags.Instance | BindingFlags.Static |
  38. BindingFlags.Public | BindingFlags.NonPublic;
  39. readonly string _memberName;
  40. readonly string _memberSignature;
  41. readonly MemberTypes _memberType;
  42. readonly Type _reflectedType;
  43. readonly Type[] _genericArguments;
  44. MemberInfoSerializationHolder(SerializationInfo info, StreamingContext ctx)
  45. {
  46. string assemblyName;
  47. string typeName;
  48. assemblyName = info.GetString("AssemblyName");
  49. typeName = info.GetString("ClassName");
  50. _memberName = info.GetString("Name");
  51. _memberSignature = info.GetString("Signature");
  52. _memberType = (MemberTypes) info.GetInt32("MemberType");
  53. try {
  54. _genericArguments = null;
  55. // FIXME: this doesn't work at present. It seems that
  56. // ObjectManager doesn't cope with nested IObjectReferences.
  57. // _genericArguments = (Type[]) info.GetValue("GenericArguments", typeof(Type[]));
  58. } catch (SerializationException) {
  59. // expected (old NET_1_0 protocol)
  60. }
  61. // Load type
  62. Assembly asm = Assembly.Load(assemblyName);
  63. _reflectedType = asm.GetType(typeName, true, true);
  64. }
  65. public static void Serialize(SerializationInfo info, String name, Type klass, String signature, MemberTypes type)
  66. {
  67. Serialize (info, name, klass, signature, type, null);
  68. }
  69. public static void Serialize(SerializationInfo info, String name, Type klass, String signature, MemberTypes type, Type[] genericArguments)
  70. {
  71. info.SetType( typeof(MemberInfoSerializationHolder));
  72. info.AddValue("AssemblyName", klass.Module.Assembly.FullName, typeof(String));
  73. info.AddValue("ClassName", klass.FullName, typeof(String));
  74. info.AddValue("Name", name, typeof(String));
  75. info.AddValue("Signature", signature, typeof(String));
  76. info.AddValue("MemberType",(int)type);
  77. info.AddValue("GenericArguments", genericArguments, typeof (Type[]));
  78. }
  79. public void GetObjectData(SerializationInfo info, StreamingContext context)
  80. {
  81. throw new NotSupportedException();
  82. }
  83. public object GetRealObject(StreamingContext context)
  84. {
  85. switch (_memberType)
  86. {
  87. case MemberTypes.Constructor:
  88. ConstructorInfo [] ctors;
  89. ctors = _reflectedType.GetConstructors (DefaultBinding);
  90. for (int i = 0; i < ctors.Length; i++)
  91. if ( ctors[i].ToString().Equals(_memberSignature))
  92. return ctors[i];
  93. throw new SerializationException (String.Format ("Could not find constructor '{0}' in type '{1}'", _memberSignature, _reflectedType));
  94. case MemberTypes.Method:
  95. MethodInfo [] methods;
  96. methods = _reflectedType.GetMethods(DefaultBinding);
  97. for (int i = 0; i < methods.Length; i++)
  98. if ((methods[i]).ToString().Equals(_memberSignature))
  99. return methods[i];
  100. else if (_genericArguments != null &&
  101. methods[i].IsGenericMethod &&
  102. methods[i].GetGenericArguments().Length == _genericArguments.Length) {
  103. MethodInfo mi = methods[i].MakeGenericMethod(_genericArguments);
  104. if (mi.ToString() == _memberSignature)
  105. return mi;
  106. }
  107. throw new SerializationException (String.Format ("Could not find method '{0}' in type '{1}'", _memberSignature, _reflectedType));
  108. case MemberTypes.Field:
  109. FieldInfo fi = _reflectedType.GetField (_memberName, DefaultBinding);
  110. if (fi != null)
  111. return fi;
  112. throw new SerializationException (String.Format ("Could not find field '{0}' in type '{1}'", _memberName, _reflectedType));
  113. case MemberTypes.Property:
  114. PropertyInfo pi = _reflectedType.GetProperty (_memberName, DefaultBinding);
  115. if (pi != null)
  116. return pi;
  117. throw new SerializationException (String.Format ("Could not find property '{0}' in type '{1}'", _memberName, _reflectedType));
  118. case MemberTypes.Event:
  119. EventInfo ei = _reflectedType.GetEvent (_memberName, DefaultBinding);
  120. if (ei != null)
  121. return ei;
  122. throw new SerializationException (String.Format ("Could not find event '{0}' in type '{1}'", _memberName, _reflectedType));
  123. default:
  124. throw new SerializationException (String.Format ("Unhandled MemberType {0}", _memberType));
  125. }
  126. }
  127. }
  128. }