Prechádzať zdrojové kódy

2004-11-29 Lluis Sanchez Gual <[email protected]>

	* CodeGenerator.cs: Addded EnumToUnderlying method to get the underlying
	type of an enum. This fixes bug #69753.


svn path=/trunk/mcs/; revision=36765
Lluis Sanchez 21 rokov pred
rodič
commit
05dca65e99

+ 5 - 0
mcs/class/corlib/System.Runtime.Serialization.Formatters.Binary/ChangeLog

@@ -1,3 +1,8 @@
+2004-11-29  Lluis Sanchez Gual  <[email protected]>
+
+	* CodeGenerator.cs: Addded EnumToUnderlying method to get the underlying
+	type of an enum. This fixes bug #69753.
+
 2004-07-02  Lluis Sanchez Gual  <[email protected]>
 
 	* BinaryCommon.cs: Added CheckSerializable method.

+ 35 - 1
mcs/class/corlib/System.Runtime.Serialization.Formatters.Binary/CodeGenerator.cs

@@ -250,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
@@ -359,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);
+		}
 	}
  }