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
;
39 internal class DelegateSerializationHolder
: ISerializable
, IObjectReference
41 Delegate _delegate
; // The deserialized delegate
49 string targetTypeAssembly
;
50 string targetTypeName
;
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
;
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;
70 realTarget
= info
.GetValue (target
.ToString(), typeof(object));
72 Assembly dasm
= Assembly
.Load (assembly
);
73 Type dt
= dasm
.GetType (type
);
75 if (realTarget
!= null)
76 del
= Delegate
.CreateDelegate (dt
, realTarget
, methodName
);
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."));
91 DelegateSerializationHolder(SerializationInfo info
, StreamingContext ctx
)
93 DelegateEntry entryChain
= (DelegateEntry
)info
.GetValue ("Delegate", typeof(DelegateEntry
));
95 // Count the number of delegates to combine
97 DelegateEntry entry
= entryChain
;
98 while (entry
!= null) {
99 entry
= entry
.delegateEntry
;
103 // Deserializes and combines the delegates
105 _delegate
= entryChain
.DeserializeDelegate (info
);
108 Delegate
[] delegates
= new Delegate
[count
];
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
);
133 lastEntry
.delegateEntry
= 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
)
145 throw new NotSupportedException ();
148 public object GetRealObject (StreamingContext context
)