2005-01-31 Zoltan Varga <vargaz@freemail.hu>
[mono.git] / mcs / class / corlib / System / UnitySerializationHolder.cs
1 //
2 // System.UnitySerializationHolder.cs
3 //
4 // Author:
5 //   Lluis Sanchez Gual (lsg@ctv.es)
6 //
7 // (C) 2003 Lluis Sanchez Gual
8
9 //
10 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
11 //
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:
19 // 
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
22 // 
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.
30 //
31
32 using System;
33 using System.Reflection;
34 using System.Runtime.Serialization;
35
36 namespace System
37 {
38         [Serializable]
39         internal class UnitySerializationHolder : IObjectReference, ISerializable
40         {
41                 string _data;
42                 UnityType _unityType;
43                 string _assemblyName;
44
45                 // FIXME: there must be other types that use UnitySerializationHolder for
46                 // serialization, but I don't know yet which ones.
47
48                 enum UnityType: byte
49                 {
50                         DBNull = 2,
51                         Type = 4,
52                         Module = 5,
53                         Assembly = 6
54                 }
55
56                 UnitySerializationHolder (SerializationInfo info, StreamingContext ctx)
57                 {
58                         _data = info.GetString ("Data");
59                         _unityType = (UnityType) info.GetInt32 ("UnityType");
60                         _assemblyName = info.GetString ("AssemblyName");
61                 }
62
63                 public static void GetTypeData (Type instance, SerializationInfo info, StreamingContext ctx)
64                 {
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));
69                 }
70
71                 public static void GetDBNullData (DBNull instance, SerializationInfo info, StreamingContext ctx)
72                 {
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));
77                 }
78
79                 public static void GetAssemblyData (Assembly instance, SerializationInfo info, StreamingContext ctx)
80                 {
81                         info.AddValue ("Data", instance.FullName);
82                         info.AddValue ("UnityType", (int) UnityType.Assembly);
83                         info.AddValue ("AssemblyName", instance.FullName);
84                         info.SetType (typeof (UnitySerializationHolder));
85                 }
86
87                 public static void GetModuleData (Module instance, SerializationInfo info, StreamingContext ctx)
88                 {
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));
93                 }
94
95                 public virtual void GetObjectData (SerializationInfo info, StreamingContext context)
96                 {
97                         // Not needed.
98                         throw new NotSupportedException();
99                 }
100
101                 public virtual object GetRealObject (StreamingContext context)
102                 {
103                         switch (_unityType) {
104                         case UnityType.Type: {
105                                 Assembly assembly = Assembly.Load (_assemblyName);
106                                 return assembly.GetType (_data);
107                         }
108                         case UnityType.DBNull:
109                                 return DBNull.Value;
110                         case UnityType.Module: {
111                                 Assembly assembly = Assembly.Load (_assemblyName);
112                                 return assembly.GetModule (_data);
113                         }
114                         case UnityType.Assembly:
115                                 return Assembly.Load (_data);
116                         default:
117                                 throw new NotSupportedException (Locale.GetText
118                                         ("UnitySerializationHolder does not support this type."));
119                         }
120                 }
121         }
122 }