2005-03-13 Martin Baulig <martin@ximian.com>
[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
36 namespace System
37 {
38         [Serializable]
39         internal class DelegateSerializationHolder: ISerializable, IObjectReference
40         {
41                 Delegate _delegate; // The deserialized delegate
42
43                 [Serializable]
44                 class DelegateEntry
45                 {
46                         string type;
47                         string assembly;
48                         public object target;
49                         string targetTypeAssembly;
50                         string targetTypeName;
51                         string methodName;
52                         public DelegateEntry delegateEntry; // next delegate in the invocation list
53
54                         // A DelegateEntry holds information about a delegate that is part
55                         // of an invocation list of a multicast delegate.
56                         public DelegateEntry (Delegate del, string targetLabel)
57                         {
58                                 type = del.GetType().FullName;
59                                 assembly = del.GetType().Assembly.FullName;
60                                 target = targetLabel;
61                                 targetTypeAssembly = del.Method.DeclaringType.Assembly.FullName;
62                                 targetTypeName = del.Method.DeclaringType.FullName;
63                                 methodName = del.Method.Name;
64                         }
65
66                         public Delegate DeserializeDelegate (SerializationInfo info)
67                         {
68                                 object realTarget = null;
69                                 if (target != null)
70                                         realTarget = info.GetValue (target.ToString(), typeof(object));
71
72                                 Assembly dasm = Assembly.Load (assembly);
73                                 Type dt = dasm.GetType (type);
74                                 Delegate del;
75                                 if (realTarget != null)
76                                         del = Delegate.CreateDelegate (dt, realTarget, methodName);
77                                 else {
78                                         Assembly tasm = Assembly.Load (targetTypeAssembly);
79                                         Type tt = tasm.GetType (targetTypeName);
80                                         del = Delegate.CreateDelegate (dt, tt, methodName);
81                                 }
82
83                                 if (!del.Method.IsPublic)
84                                         throw new SerializationException (Locale.GetText (
85                                                 "Serialization will not deserialize delegates to non-public methods."));
86
87                                 return del;
88                         }
89                 }
90
91                 DelegateSerializationHolder(SerializationInfo info, StreamingContext ctx)
92                 {
93                         DelegateEntry entryChain = (DelegateEntry)info.GetValue ("Delegate", typeof(DelegateEntry));
94
95                         // Count the number of delegates to combine
96                         int count = 0;
97                         DelegateEntry entry = entryChain;
98                         while (entry != null) {
99                                 entry = entry.delegateEntry;
100                                 count++;
101                         }
102
103                         // Deserializes and combines the delegates
104                         if (count == 1) 
105                                 _delegate = entryChain.DeserializeDelegate (info);
106                         else
107                         {
108                                 Delegate[] delegates = new Delegate[count];
109                                 entry = entryChain;
110                                 for (int n=0; n<count; n++)
111                                 {
112                                         delegates[n] = entry.DeserializeDelegate (info);
113                                         entry = entry.delegateEntry;
114                                 }
115                                 _delegate = Delegate.Combine (delegates);
116                         }
117                 }
118
119                 public static void GetDelegateData (Delegate instance, SerializationInfo info, StreamingContext ctx)
120                 {
121                         // Fills a SerializationInfo object with the information of the delegate.
122
123                         Delegate[] delegates = instance.GetInvocationList ();
124                         DelegateEntry lastEntry = null;
125                         for (int n=0; n<delegates.Length; n++) {
126                                 Delegate del = delegates[n];
127                                 string targetLabel = (del.Target != null) ? ("target" + n) : null;
128                                 DelegateEntry entry = new DelegateEntry (del, targetLabel);
129
130                                 if (lastEntry == null)
131                                         info.AddValue ("Delegate", entry);
132                                 else
133                                         lastEntry.delegateEntry = entry;
134
135                                 lastEntry = entry;
136                                 if (del.Target != null)
137                                         info.AddValue (targetLabel, del.Target);
138                         }
139                         info.SetType (typeof (DelegateSerializationHolder));
140                 }
141
142                 public void GetObjectData (SerializationInfo info, StreamingContext context)
143                 {
144                         // Not needed.
145                         throw new NotSupportedException ();
146                 }
147
148                 public object GetRealObject (StreamingContext context)
149                 {
150                         return _delegate;
151                 }
152         }
153 }