2 // System.DelegateSerializationHolder.cs
5 // Lluis Sanchez Gual (lsg@ctv.es)
7 // (C) 2003 Lluis Sanchez Gual
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:
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
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;
35 using System.Runtime.Remoting;
40 internal class DelegateSerializationHolder: ISerializable, IObjectReference
42 Delegate _delegate; // The deserialized delegate
50 string targetTypeAssembly;
51 string targetTypeName;
53 public DelegateEntry delegateEntry; // next delegate in the invocation list
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)
59 type = del.GetType().FullName;
60 assembly = del.GetType().Assembly.FullName;
62 targetTypeAssembly = del.Method.DeclaringType.Assembly.FullName;
63 targetTypeName = del.Method.DeclaringType.FullName;
64 methodName = del.Method.Name;
67 public Delegate DeserializeDelegate (SerializationInfo info)
69 object realTarget = null;
71 realTarget = info.GetValue (target.ToString(), typeof(object));
73 Assembly dasm = Assembly.Load (assembly);
74 Type dt = dasm.GetType (type);
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.");
87 del = Delegate.CreateDelegate (dt, realTarget, methodName);
90 Assembly tasm = Assembly.Load (targetTypeAssembly);
91 Type tt = tasm.GetType (targetTypeName);
92 del = Delegate.CreateDelegate (dt, tt, methodName);
95 if (!del.Method.IsPublic)
96 throw new SerializationException (Locale.GetText (
97 "Serialization will not deserialize delegates to non-public methods."));
103 DelegateSerializationHolder(SerializationInfo info, StreamingContext ctx)
105 DelegateEntry entryChain = (DelegateEntry)info.GetValue ("Delegate", typeof(DelegateEntry));
107 // Count the number of delegates to combine
109 DelegateEntry entry = entryChain;
110 while (entry != null) {
111 entry = entry.delegateEntry;
115 // Deserializes and combines the delegates
117 _delegate = entryChain.DeserializeDelegate (info);
120 Delegate[] delegates = new Delegate[count];
122 for (int n=0; n<count; n++)
124 delegates[n] = entry.DeserializeDelegate (info);
125 entry = entry.delegateEntry;
127 _delegate = Delegate.Combine (delegates);
131 public static void GetDelegateData (Delegate instance, SerializationInfo info, StreamingContext ctx)
133 // Fills a SerializationInfo object with the information of the delegate.
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);
142 if (lastEntry == null)
143 info.AddValue ("Delegate", entry);
145 lastEntry.delegateEntry = entry;
148 if (del.Target != null)
149 info.AddValue (targetLabel, del.Target);
151 info.SetType (typeof (DelegateSerializationHolder));
154 public void GetObjectData (SerializationInfo info, StreamingContext context)
157 throw new NotSupportedException ();
160 public object GetRealObject (StreamingContext context)