2 // System.UnitySerializationHolder.cs
5 // Lluis Sanchez Gual (lsg@ctv.es)
7 // (C) 2003 Lluis Sanchez Gual
10 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 using System.Reflection;
34 using System.Runtime.Serialization;
39 internal class UnitySerializationHolder : IObjectReference, ISerializable
45 // FIXME: there must be other types that use UnitySerializationHolder for
46 // serialization, but I don't know yet which ones.
56 UnitySerializationHolder (SerializationInfo info, StreamingContext ctx)
58 _data = info.GetString ("Data");
59 _unityType = (UnityType) info.GetInt32 ("UnityType");
60 _assemblyName = info.GetString ("AssemblyName");
63 public static void GetTypeData (Type instance, SerializationInfo info, StreamingContext ctx)
65 info.AddValue ("Data", instance.FullName);
66 info.AddValue ("UnityType", (int) UnityType.Type);
67 info.AddValue ("AssemblyName", instance.Assembly.FullName);
68 info.SetType (typeof (UnitySerializationHolder));
71 public static void GetDBNullData (DBNull instance, SerializationInfo info, StreamingContext ctx)
73 info.AddValue ("Data", null);
74 info.AddValue ("UnityType", (int) UnityType.DBNull);
75 info.AddValue ("AssemblyName", instance.GetType().Assembly.FullName);
76 info.SetType (typeof (UnitySerializationHolder));
79 public static void GetAssemblyData (Assembly instance, SerializationInfo info, StreamingContext ctx)
81 info.AddValue ("Data", instance.FullName);
82 info.AddValue ("UnityType", (int) UnityType.Assembly);
83 info.AddValue ("AssemblyName", instance.FullName);
84 info.SetType (typeof (UnitySerializationHolder));
87 public static void GetModuleData (Module instance, SerializationInfo info, StreamingContext ctx)
89 info.AddValue ("Data", instance.ScopeName);
90 info.AddValue ("UnityType", (int) UnityType.Module);
91 info.AddValue ("AssemblyName", instance.Assembly.FullName);
92 info.SetType (typeof (UnitySerializationHolder));
95 public virtual void GetObjectData (SerializationInfo info, StreamingContext context)
98 throw new NotSupportedException();
101 public virtual object GetRealObject (StreamingContext context)
103 switch (_unityType) {
104 case UnityType.Type: {
105 Assembly assembly = Assembly.Load (_assemblyName);
106 return assembly.GetType (_data);
108 case UnityType.DBNull:
110 case UnityType.Module: {
111 Assembly assembly = Assembly.Load (_assemblyName);
112 return assembly.GetModule (_data);
114 case UnityType.Assembly:
115 return Assembly.Load (_data);
117 throw new NotSupportedException (Locale.GetText
118 ("UnitySerializationHolder does not support this type."));