[LoongArch64] Part-5:add loongarch support in some files for LoongArch64. (#21769)
[mono-project.git] / mcs / class / corlib / System / DelegateSerializationHolder.cs
blobe4b4a6f2c1169babd92f8019537d6ca94905716a
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;
35 using System.Runtime.Remoting;
37 namespace System
39 [Serializable]
40 internal class DelegateSerializationHolder: ISerializable, IObjectReference
42 Delegate _delegate; // The deserialized delegate
44 [Serializable]
45 class DelegateEntry
47 string type;
48 string assembly;
49 object target;
50 string targetTypeAssembly;
51 string targetTypeName;
52 string methodName;
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;
61 target = targetLabel;
62 targetTypeAssembly = del.Method.DeclaringType.Assembly.FullName;
63 targetTypeName = del.Method.DeclaringType.FullName;
64 methodName = del.Method.Name;
67 public Delegate DeserializeDelegate (SerializationInfo info, int index)
69 object realTarget = null;
70 if (target != null)
71 realTarget = info.GetValue (target.ToString(), typeof(object));
73 var key = "method" + index;
74 var method = (MethodInfo)info.GetValueNoThrow (key, typeof(MethodInfo));
76 Assembly dasm = Assembly.Load (assembly);
77 Type dt = dasm.GetType (type);
79 if (realTarget != null) {
80 #if FEATURE_REMOTING
81 if (RemotingServices.IsTransparentProxy (realTarget)) {
82 // The call to IsInstanceOfType will force the proxy
83 // to load the real type of the remote object. This is
84 // needed to make sure that subsequent calls to
85 // GetType() return the expected type.
86 Assembly tasm = Assembly.Load (targetTypeAssembly);
87 Type tt = tasm.GetType (targetTypeName);
88 if (!tt.IsInstanceOfType (realTarget))
89 throw new RemotingException ("Unexpected proxy type.");
91 #endif
92 return method == null ?
93 Delegate.CreateDelegate (dt, realTarget, methodName) :
94 Delegate.CreateDelegate (dt, realTarget, method);
97 if (method != null)
98 return Delegate.CreateDelegate (dt, realTarget, method);
100 Type tt2 = Assembly.Load (targetTypeAssembly).GetType (targetTypeName);
101 return Delegate.CreateDelegate (dt, tt2, methodName);
105 DelegateSerializationHolder(SerializationInfo info, StreamingContext ctx)
107 DelegateEntry entryChain = (DelegateEntry)info.GetValue ("Delegate", typeof(DelegateEntry));
109 // Count the number of delegates to combine
110 int count = 0;
111 DelegateEntry entry = entryChain;
112 while (entry != null) {
113 entry = entry.delegateEntry;
114 count++;
117 // Deserializes and combines the delegates
118 if (count == 1)
119 _delegate = entryChain.DeserializeDelegate (info, 0);
120 else
122 Delegate[] delegates = new Delegate[count];
123 entry = entryChain;
124 for (int n=0; n<count; n++)
126 delegates[n] = entry.DeserializeDelegate (info, n);
127 entry = entry.delegateEntry;
129 _delegate = Delegate.Combine (delegates);
133 public static void GetDelegateData (Delegate instance, SerializationInfo info, StreamingContext ctx)
135 // Fills a SerializationInfo object with the information of the delegate.
137 Delegate[] delegates = instance.GetInvocationList ();
138 DelegateEntry lastEntry = null;
139 for (int n=0; n<delegates.Length; n++) {
140 Delegate del = delegates[n];
141 string targetLabel = (del.Target != null) ? ("target" + n) : null;
142 DelegateEntry entry = new DelegateEntry (del, targetLabel);
144 if (lastEntry == null)
145 info.AddValue ("Delegate", entry);
146 else
147 lastEntry.delegateEntry = entry;
149 lastEntry = entry;
150 if (del.Target != null)
151 info.AddValue (targetLabel, del.Target);
153 info.AddValue ("method" + n, del.Method);
155 info.SetType (typeof (DelegateSerializationHolder));
158 public void GetObjectData (SerializationInfo info, StreamingContext context)
160 // Not needed.
161 throw new NotSupportedException ();
164 public object GetRealObject (StreamingContext context)
166 return _delegate;