**** Merged from MCS ****
[mono-project.git] / mcs / class / corlib / System / DelegateSerializationHolder.cs
blob1e30141db33ec3ae4ccbb5990de4869febf785e4
1 //
2 // System.DelegateSerializationHolder.cs
3 //
4 // Author:
5 // Lluis Sanchez Gual (lsg@ctv.es)
6 //
7 // (C) 2003 Lluis Sanchez Gual
8 //
11 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
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.
33 using System.Reflection;
34 using System.Runtime.Serialization;
36 namespace System
38 [Serializable]
39 internal class DelegateSerializationHolder: ISerializable, IObjectReference
41 Delegate _delegate; // The deserialized delegate
43 [Serializable]
44 class DelegateEntry
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
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)
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;
66 public Delegate DeserializeDelegate (SerializationInfo info)
68 object realTarget = null;
69 if (target != null)
70 realTarget = info.GetValue (target.ToString(), typeof(object));
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);
83 if (!del.Method.IsPublic)
84 throw new SerializationException (Locale.GetText (
85 "Serialization will not deserialize delegates to non-public methods."));
87 return del;
91 DelegateSerializationHolder(SerializationInfo info, StreamingContext ctx)
93 DelegateEntry entryChain = (DelegateEntry)info.GetValue ("Delegate", typeof(DelegateEntry));
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++;
103 // Deserializes and combines the delegates
104 if (count == 1)
105 _delegate = entryChain.DeserializeDelegate (info);
106 else
108 Delegate[] delegates = new Delegate[count];
109 entry = entryChain;
110 for (int n=0; n<count; n++)
112 delegates[n] = entry.DeserializeDelegate (info);
113 entry = entry.delegateEntry;
115 _delegate = Delegate.Combine (delegates);
119 public static void GetDelegateData (Delegate instance, SerializationInfo info, StreamingContext ctx)
121 // Fills a SerializationInfo object with the information of the delegate.
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);
130 if (lastEntry == null)
131 info.AddValue ("Delegate", entry);
132 else
133 lastEntry.delegateEntry = entry;
135 lastEntry = entry;
136 if (del.Target != null)
137 info.AddValue (targetLabel, del.Target);
139 info.SetType (typeof (DelegateSerializationHolder));
142 public void GetObjectData (SerializationInfo info, StreamingContext context)
144 // Not needed.
145 throw new NotSupportedException ();
148 public object GetRealObject (StreamingContext context)
150 return _delegate;