Procházet zdrojové kódy

2007-08-30 Ivan N. Zlatev <[email protected]>

* EnumConverter.cs: Implemented conversion to InstanceDescriptor
for enums with a FlagsAttribute. Fixes #82118.


svn path=/trunk/mcs/; revision=85069

Ivan Zlatev před 18 roky
rodič
revize
e503bc3377

+ 5 - 0
mcs/class/System/System.ComponentModel/ChangeLog

@@ -1,3 +1,8 @@
+2007-08-30  Ivan N. Zlatev  <[email protected]>
+	
+	* EnumConverter.cs: Implemented conversion to InstanceDescriptor
+	for enums with a FlagsAttribute. Fixes #82118.
+
 2007-08-29  Ivan N. Zlatev  <[email protected]>
 
 	* DesignerOptionService.cs: implemented.

+ 15 - 6
mcs/class/System/System.ComponentModel/EnumConverter.cs

@@ -83,12 +83,21 @@ namespace System.ComponentModel
 
 				return Enum.Format (type, value, "G");
 			} else if (destinationType == typeof (InstanceDescriptor) && value != null) {
-				string fieldName = ConvertToString (context,
-					culture, value);
-				FieldInfo f = type.GetField (fieldName);
-				if (f == null)
-					throw CreateValueNotValidException (value);
-				return new InstanceDescriptor (f, null);
+				string enumString = ConvertToString (context, culture, value);
+				if (type.IsDefined (typeof (FlagsAttribute), false) && enumString.IndexOf (",") != -1) {
+					if (value is IConvertible) {
+						Type underlyingType = Enum.GetUnderlyingType (type);
+						object enumValue = ((IConvertible)value).ToType (underlyingType, culture);
+						MethodInfo toObjectMethod = typeof (Enum).GetMethod ("ToObject", new Type[] { typeof (Type), 
+																			  underlyingType });
+						return new InstanceDescriptor (toObjectMethod, new object[] { type, enumValue });
+					}
+				} else {
+					FieldInfo f = type.GetField (enumString);
+					if (f == null)
+						throw CreateValueNotValidException (value);
+					return new InstanceDescriptor (f, null);
+				}
 #if NET_2_0
 			} else if (destinationType == typeof (Enum[]) && value != null) {
 				if (!type.IsDefined (typeof (FlagsAttribute), false)) {