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