2004-12-08 Zoltan Varga <vargaz@freemail.hu>
[mono.git] / mcs / class / corlib / System.Runtime.Serialization.Formatters.Binary / CodeGenerator.cs
index d26c4a2ea8cb417839d517ec452f07b13ec662b5..f03da9b74d14fcde3eb25e158b17fce13c018dc0 100644 (file)
@@ -5,12 +5,36 @@
 //
 // (C) 2004 Novell, Inc
 
+//
+// Copyright (C) 2004 Novell, Inc (http://www.novell.com)
+//
+// Permission is hereby granted, free of charge, to any person obtaining
+// a copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to
+// permit persons to whom the Software is furnished to do so, subject to
+// the following conditions:
+// 
+// The above copyright notice and this permission notice shall be
+// included in all copies or substantial portions of the Software.
+// 
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+//
+
 using System;
 using System.IO;
 using System.Collections;
 using System.Runtime.Serialization;
 using System.Reflection;
 using System.Reflection.Emit;
+using System.Globalization;
 
 namespace System.Runtime.Serialization.Formatters.Binary
 {
@@ -31,7 +55,7 @@ namespace System.Runtime.Serialization.Formatters.Binary
                                                AssemblyName myAsmName = new AssemblyName();
                                                myAsmName.Name = "__MetadataTypes";
                                           
-                                               AssemblyBuilder myAsmBuilder = myDomain.DefineDynamicAssembly (myAsmName, AssemblyBuilderAccess.Run);
+                                               AssemblyBuilder myAsmBuilder = myDomain.DefineInternalDynamicAssembly (myAsmName, AssemblyBuilderAccess.Run);
                                                _module = myAsmBuilder.DefineDynamicModule("__MetadataTypesModule", true);
                                        }
                                }
@@ -168,7 +192,7 @@ namespace System.Runtime.Serialization.Formatters.Binary
                                {
                                        gen.Emit (OpCodes.Ldarg_2);
                                        gen.Emit (lload, localBuilder);
-                                       if (ftype == typeof(DateTime) || ftype == typeof(TimeSpan))
+                                       if (ftype == typeof(DateTime) || ftype == typeof(TimeSpan) || ftype == typeof(decimal))
                                                gen.Emit (OpCodes.Ldflda, field);
                                        else
                                                gen.Emit (OpCodes.Ldfld, field);
@@ -226,7 +250,7 @@ namespace System.Runtime.Serialization.Formatters.Binary
                                if (t == typeof(Enum))
                                        ig.Emit (OpCodes.Ldind_Ref);
                                else
-                                       LoadFromPtr (ig, t.UnderlyingSystemType);
+                                       LoadFromPtr (ig, EnumToUnderlying (t));
                        } else if (t.IsValueType)
                                ig.Emit (OpCodes.Ldobj, t);
                        else
@@ -300,7 +324,6 @@ namespace System.Runtime.Serialization.Formatters.Binary
                                case TypeCode.Boolean:
                                case TypeCode.Byte:
                                case TypeCode.Char:
-                               case TypeCode.Decimal:
                                case TypeCode.Double:
                                case TypeCode.Int16:
                                case TypeCode.Int32:
@@ -314,6 +337,13 @@ namespace System.Runtime.Serialization.Formatters.Binary
                                        EmitWrite (gen, type);
                                        break;
 
+                               case TypeCode.Decimal:
+                                       // writer.Write (val.ToString (CultureInfo.InvariantCulture));
+                                       gen.EmitCall (OpCodes.Call, typeof(CultureInfo).GetProperty("InvariantCulture").GetGetMethod(), null);
+                                       gen.EmitCall (OpCodes.Call, typeof(Decimal).GetMethod("ToString", new Type[] {typeof(IFormatProvider)}), null);
+                                       EmitWrite (gen, typeof(string));
+                                       break;
+                                       
                                case TypeCode.DateTime: 
                                        gen.EmitCall (OpCodes.Call, typeof(DateTime).GetProperty("Ticks").GetGetMethod(), null);
                                        EmitWrite (gen, typeof(long));
@@ -329,6 +359,40 @@ namespace System.Runtime.Serialization.Formatters.Binary
                                        break;
                        }
                }
+               
+               //
+               // This is needed, because enumerations from assemblies
+               // do not report their underlyingtype, but they report
+               // themselves
+               //
+               public static Type EnumToUnderlying (Type t)
+               {
+                       TypeCode tc = Type.GetTypeCode (t);
+       
+                       switch (tc){
+                       case TypeCode.Boolean:
+                               return typeof (bool);
+                       case TypeCode.Byte:
+                               return typeof (byte);
+                       case TypeCode.SByte:
+                               return typeof (sbyte);
+                       case TypeCode.Char:
+                               return typeof (char);
+                       case TypeCode.Int16:
+                               return typeof (short);
+                       case TypeCode.UInt16:
+                               return typeof (ushort);
+                       case TypeCode.Int32:
+                               return typeof (int);
+                       case TypeCode.UInt32:
+                               return typeof (uint);
+                       case TypeCode.Int64:
+                               return typeof (long);
+                       case TypeCode.UInt64:
+                               return typeof (ulong);
+                       }
+                       throw new Exception ("Unhandled typecode in enum " + tc + " from " + t.AssemblyQualifiedName);
+               }
        }
  }