New test.
[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 #if NET_2_0
48                 readonly Type[]          _genericArguments;
49 #endif
50                 MemberInfoSerializationHolder(SerializationInfo info, StreamingContext ctx)
51                 {
52                         string assemblyName;
53                         string typeName;
54
55                         assemblyName = info.GetString("AssemblyName");
56                         typeName = info.GetString("ClassName");
57
58                         _memberName = info.GetString("Name");
59                         _memberSignature = info.GetString("Signature");
60                         _memberType = (MemberTypes) info.GetInt32("MemberType");
61
62 #if NET_2_0
63                         try {
64                                 _genericArguments = null;
65
66                                 // FIXME: this doesn't work at present. It seems that
67                                 // ObjectManager doesn't cope with nested IObjectReferences.
68                                 // _genericArguments = (Type[]) info.GetValue("GenericArguments", typeof(Type[]));
69                         } catch (SerializationException) {
70                                 // expected (old NET_1_0 protocol)
71                         }
72 #endif
73                         // Load type
74                         Assembly asm = Assembly.Load(assemblyName);
75
76                         _reflectedType = asm.GetType(typeName, true, true);
77                 }
78
79                 public static void Serialize(SerializationInfo info, String name, Type klass, String signature, MemberTypes type)
80                 {
81                         Serialize (info, name, klass, signature, type, null);
82                 }
83
84 #if NET_2_0
85                 public
86 #endif
87                 static void Serialize(SerializationInfo info, String name, Type klass, String signature, MemberTypes type, Type[] genericArguments)
88                 {
89                         info.SetType( typeof(MemberInfoSerializationHolder));
90
91                         info.AddValue("AssemblyName", klass.Module.Assembly.FullName, typeof(String));
92                         info.AddValue("ClassName", klass.FullName, typeof(String));
93
94                         info.AddValue("Name", name, typeof(String));
95                         info.AddValue("Signature", signature, typeof(String));
96                         info.AddValue("MemberType",(int)type);
97 #if NET_2_0
98                         info.AddValue("GenericArguments", genericArguments, typeof (Type[]));
99 #endif
100                 }
101
102                 public void GetObjectData(SerializationInfo info, StreamingContext context)
103                 {
104                         throw new NotSupportedException();
105                 }
106
107                 public object GetRealObject(StreamingContext context)
108                 {
109                         switch (_memberType) 
110                         {
111                                 case MemberTypes.Constructor:
112                                         ConstructorInfo [] ctors;
113
114                                         ctors = _reflectedType.GetConstructors (DefaultBinding);
115                                         for (int i = 0; i < ctors.Length; i++) 
116                                                 if ( ctors[i].ToString().Equals(_memberSignature)) 
117                                                         return ctors[i];
118
119                                         throw new SerializationException (String.Format ("Could not find constructor '{0}' in type '{1}'", _memberSignature, _reflectedType));
120
121                                 case MemberTypes.Method:
122                                         MethodInfo [] methods;
123
124                                         methods = _reflectedType.GetMethods(DefaultBinding);
125                                         for (int i = 0; i < methods.Length; i++) 
126                                                 if ((methods[i]).ToString().Equals(_memberSignature)) 
127                                                         return methods[i];
128 #if NET_2_0
129                                                 else if (_genericArguments != null &&
130                                                         methods[i].IsGenericMethod &&
131                                                         methods[i].GetGenericArguments().Length == _genericArguments.Length) {
132
133                                                         MethodInfo mi = methods[i].MakeGenericMethod(_genericArguments);
134
135                                                         if (mi.ToString() == _memberSignature)
136                                                                 return mi;
137                                                 }
138 #endif
139
140                                         throw new SerializationException (String.Format ("Could not find method '{0}' in type '{1}'", _memberSignature, _reflectedType));
141
142                                 case MemberTypes.Field:
143                                         FieldInfo fi = _reflectedType.GetField (_memberName, DefaultBinding);
144
145                                         if (fi != null)
146                                                 return fi;
147
148                                         throw new SerializationException (String.Format ("Could not find field '{0}' in type '{1}'", _memberName, _reflectedType));
149
150                                 case MemberTypes.Property:
151                                         PropertyInfo pi = _reflectedType.GetProperty (_memberName, DefaultBinding);
152
153                                         if (pi != null)
154                                                 return pi;
155
156                                         throw new SerializationException (String.Format ("Could not find property '{0}' in type '{1}'", _memberName, _reflectedType));
157
158 #if NET_2_0
159                                 case MemberTypes.Event:
160                                         EventInfo ei = _reflectedType.GetEvent (_memberName, DefaultBinding);
161
162                                         if (ei != null)
163                                                 return ei;
164
165                                         throw new SerializationException (String.Format ("Could not find event '{0}' in type '{1}'", _memberName, _reflectedType));
166 #endif
167
168                                 default:
169                                         throw new SerializationException (String.Format ("Unhandled MemberType {0}",  _memberType));
170                         }
171                 }
172         }
173 }