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