[S.R.Serialization] implemented interpreter-based XmlFormatWriterGenerator.
[mono.git] / mcs / class / System.Runtime.Serialization / ReferenceSources / CodeInterpreter.cs
1
2 namespace System.Runtime.Serialization
3 {
4         public class CodeInterpreter
5         {
6
7                 internal static object ConvertValue(object arg, Type source, Type target)
8                 {
9                         return InternalConvert(arg, source, target, false);
10                 }
11
12
13         static bool CanConvert (TypeCode typeCode)
14         {
15             switch (typeCode)
16             {
17                 case TypeCode.Boolean:
18                 case TypeCode.Char:
19                 case TypeCode.SByte:
20                 case TypeCode.Byte:
21                 case TypeCode.Int16:
22                 case TypeCode.UInt16:
23                 case TypeCode.Int32:
24                 case TypeCode.UInt32:
25                 case TypeCode.Int64:
26                 case TypeCode.UInt64:
27                 case TypeCode.Single:
28                 case TypeCode.Double:
29                                         return true;
30             }
31             return false;
32         }
33
34                 static object InternalConvert(object arg, Type source, Type target, bool isAddress)
35                 {
36
37             if (target == source)
38                 return arg;
39             if (target.IsValueType)
40             {
41                 if (source.IsValueType)
42                 {
43                     if (CanConvert (Type.GetTypeCode(target)))
44                         throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(XmlObjectSerializer.CreateSerializationException(SR.GetString(SR.NoConversionPossibleTo, DataContract.GetClrTypeFullName(target))));
45                     else
46                                                 return target;
47                 }
48                 else if (source.IsAssignableFrom(target))
49                                         return arg;
50                 else
51                     throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(XmlObjectSerializer.CreateSerializationException(SR.GetString(SR.IsNotAssignableFrom, DataContract.GetClrTypeFullName(target), DataContract.GetClrTypeFullName(source))));
52             }
53             else if (target.IsAssignableFrom(source))
54                                 return arg;
55             else if (source.IsAssignableFrom(target))
56                                 return arg;
57             else if (target.IsInterface || source.IsInterface)
58                                 return arg;
59             else
60                 throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(XmlObjectSerializer.CreateSerializationException(SR.GetString(SR.IsNotAssignableFrom, DataContract.GetClrTypeFullName(target), DataContract.GetClrTypeFullName(source))));
61                 }
62         }
63 }
64