74d704422d2b24bc746a13823b2eabc799965525
[mono.git] / mcs / class / corlib / System / DelegateSerializationHolder.cs
1 //
2 // System.DelegateSerializationHolder.cs
3 //
4 // Author:
5 //   Lluis Sanchez Gual (lsg@ctv.es)
6 //
7 // (C) 2003 Lluis Sanchez Gual
8 //
9
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
33 using System.Reflection;
34 using System.Runtime.Serialization;
35 using System.Runtime.Remoting;
36
37 namespace System
38 {
39         [Serializable]
40         internal class DelegateSerializationHolder: ISerializable, IObjectReference
41         {
42                 Delegate _delegate; // The deserialized delegate
43
44                 [Serializable]
45                 class DelegateEntry
46                 {
47                         string type;
48                         string assembly;
49                         object target;
50                         string targetTypeAssembly;
51                         string targetTypeName;
52                         string methodName;
53                         public DelegateEntry delegateEntry; // next delegate in the invocation list
54
55                         // A DelegateEntry holds information about a delegate that is part
56                         // of an invocation list of a multicast delegate.
57                         public DelegateEntry (Delegate del, string targetLabel)
58                         {
59                                 type = del.GetType().FullName;
60                                 assembly = del.GetType().Assembly.FullName;
61                                 target = targetLabel;
62                                 targetTypeAssembly = del.Method.DeclaringType.Assembly.FullName;
63                                 targetTypeName = del.Method.DeclaringType.FullName;
64                                 methodName = del.Method.Name;
65                         }
66
67                         public Delegate DeserializeDelegate (SerializationInfo info, int index)
68                         {
69                                 object realTarget = null;
70                                 if (target != null)
71                                         realTarget = info.GetValue (target.ToString(), typeof(object));
72
73                                 var key = "method" + index;
74                                 var method = info.HasKey (key) ? (MethodInfo)info.GetValue (key, typeof (MethodInfo)) : null;
75
76                                 Assembly dasm = Assembly.Load (assembly);
77                                 Type dt = dasm.GetType (type);
78
79                                 if (realTarget != null) {
80 #if !DISABLE_REMOTING
81                                         if (RemotingServices.IsTransparentProxy (realTarget)) {
82                                                 // The call to IsInstanceOfType will force the proxy
83                                                 // to load the real type of the remote object. This is
84                                                 // needed to make sure that subsequent calls to
85                                                 // GetType() return the expected type.
86                                                 Assembly tasm = Assembly.Load (targetTypeAssembly);
87                                                 Type tt = tasm.GetType (targetTypeName);
88                                                 if (!tt.IsInstanceOfType (realTarget))
89                                                         throw new RemotingException ("Unexpected proxy type.");
90                                         }
91 #endif
92                                         return method == null ?
93                                                 Delegate.CreateDelegate (dt, realTarget, methodName) :
94                                                 Delegate.CreateDelegate (dt, realTarget, method);
95                                 }
96
97                                 if (method != null)
98                                         return Delegate.CreateDelegate (dt, realTarget, method);
99
100                                 Type tt2 = Assembly.Load (targetTypeAssembly).GetType (targetTypeName);
101                                 return Delegate.CreateDelegate (dt, tt2, methodName);
102                         }
103                 }
104
105                 DelegateSerializationHolder(SerializationInfo info, StreamingContext ctx)
106                 {
107                         DelegateEntry entryChain = (DelegateEntry)info.GetValue ("Delegate", typeof(DelegateEntry));
108
109                         // Count the number of delegates to combine
110                         int count = 0;
111                         DelegateEntry entry = entryChain;
112                         while (entry != null) {
113                                 entry = entry.delegateEntry;
114                                 count++;
115                         }
116
117                         // Deserializes and combines the delegates
118                         if (count == 1) 
119                                 _delegate = entryChain.DeserializeDelegate (info, 0);
120                         else
121                         {
122                                 Delegate[] delegates = new Delegate[count];
123                                 entry = entryChain;
124                                 for (int n=0; n<count; n++)
125                                 {
126                                         delegates[n] = entry.DeserializeDelegate (info, n);
127                                         entry = entry.delegateEntry;
128                                 }
129                                 _delegate = Delegate.Combine (delegates);
130                         }
131                 }
132
133                 public static void GetDelegateData (Delegate instance, SerializationInfo info, StreamingContext ctx)
134                 {
135                         // Fills a SerializationInfo object with the information of the delegate.
136
137                         Delegate[] delegates = instance.GetInvocationList ();
138                         DelegateEntry lastEntry = null;
139                         for (int n=0; n<delegates.Length; n++) {
140                                 Delegate del = delegates[n];
141                                 string targetLabel = (del.Target != null) ? ("target" + n) : null;
142                                 DelegateEntry entry = new DelegateEntry (del, targetLabel);
143
144                                 if (lastEntry == null)
145                                         info.AddValue ("Delegate", entry);
146                                 else
147                                         lastEntry.delegateEntry = entry;
148
149                                 lastEntry = entry;
150                                 if (del.Target != null)
151                                         info.AddValue (targetLabel, del.Target);
152
153                                 info.AddValue ("method" + n, del.Method);
154                         }
155                         info.SetType (typeof (DelegateSerializationHolder));
156                 }
157
158                 public void GetObjectData (SerializationInfo info, StreamingContext context)
159                 {
160                         // Not needed.
161                         throw new NotSupportedException ();
162                 }
163
164                 public object GetRealObject (StreamingContext context)
165                 {
166                         return _delegate;
167                 }
168         }
169 }