merge r67228-r67235, r67237, r67251 and r67256-67259 to trunk (they are
[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                         public 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)
68                         {
69                                 object realTarget = null;
70                                 if (target != null)
71                                         realTarget = info.GetValue (target.ToString(), typeof(object));
72
73                                 Assembly dasm = Assembly.Load (assembly);
74                                 Type dt = dasm.GetType (type);
75                                 Delegate del;
76                                 if (realTarget != null) {
77                                         if (RemotingServices.IsTransparentProxy (realTarget)) {
78                                                 // The call to IsInstanceOfType will force the proxy
79                                                 // to load the real type of the remote object. This is
80                                                 // needed to make sure that subsequent calls to
81                                                 // GetType() return the expected type.
82                                                 Assembly tasm = Assembly.Load (targetTypeAssembly);
83                                                 Type tt = tasm.GetType (targetTypeName);
84                                                 if (!tt.IsInstanceOfType (realTarget))
85                                                         throw new RemotingException ("Unexpected proxy type.");
86                                         }
87                                         del = Delegate.CreateDelegate (dt, realTarget, methodName);
88                                 }
89                                 else {
90                                         Assembly tasm = Assembly.Load (targetTypeAssembly);
91                                         Type tt = tasm.GetType (targetTypeName);
92                                         del = Delegate.CreateDelegate (dt, tt, methodName);
93                                 }
94
95                                 if (!del.Method.IsPublic)
96                                         throw new SerializationException (Locale.GetText (
97                                                 "Serialization will not deserialize delegates to non-public methods."));
98
99                                 return del;
100                         }
101                 }
102
103                 DelegateSerializationHolder(SerializationInfo info, StreamingContext ctx)
104                 {
105                         DelegateEntry entryChain = (DelegateEntry)info.GetValue ("Delegate", typeof(DelegateEntry));
106
107                         // Count the number of delegates to combine
108                         int count = 0;
109                         DelegateEntry entry = entryChain;
110                         while (entry != null) {
111                                 entry = entry.delegateEntry;
112                                 count++;
113                         }
114
115                         // Deserializes and combines the delegates
116                         if (count == 1) 
117                                 _delegate = entryChain.DeserializeDelegate (info);
118                         else
119                         {
120                                 Delegate[] delegates = new Delegate[count];
121                                 entry = entryChain;
122                                 for (int n=0; n<count; n++)
123                                 {
124                                         delegates[n] = entry.DeserializeDelegate (info);
125                                         entry = entry.delegateEntry;
126                                 }
127                                 _delegate = Delegate.Combine (delegates);
128                         }
129                 }
130
131                 public static void GetDelegateData (Delegate instance, SerializationInfo info, StreamingContext ctx)
132                 {
133                         // Fills a SerializationInfo object with the information of the delegate.
134
135                         Delegate[] delegates = instance.GetInvocationList ();
136                         DelegateEntry lastEntry = null;
137                         for (int n=0; n<delegates.Length; n++) {
138                                 Delegate del = delegates[n];
139                                 string targetLabel = (del.Target != null) ? ("target" + n) : null;
140                                 DelegateEntry entry = new DelegateEntry (del, targetLabel);
141
142                                 if (lastEntry == null)
143                                         info.AddValue ("Delegate", entry);
144                                 else
145                                         lastEntry.delegateEntry = entry;
146
147                                 lastEntry = entry;
148                                 if (del.Target != null)
149                                         info.AddValue (targetLabel, del.Target);
150                         }
151                         info.SetType (typeof (DelegateSerializationHolder));
152                 }
153
154                 public void GetObjectData (SerializationInfo info, StreamingContext context)
155                 {
156                         // Not needed.
157                         throw new NotSupportedException ();
158                 }
159
160                 public object GetRealObject (StreamingContext context)
161                 {
162                         return _delegate;
163                 }
164         }
165 }