nicko       2005/02/03 19:05:01

  Modified:    src/Util OptionConverter.cs
  Log:
  Updated ConvertStringTo to check for a registered converter before looking 
for static Parse method
  
  Revision  Changes    Path
  1.12      +22 -20    logging-log4net/src/Util/OptionConverter.cs
  
  Index: OptionConverter.cs
  ===================================================================
  RCS file: /home/cvs/logging-log4net/src/Util/OptionConverter.cs,v
  retrieving revision 1.11
  retrieving revision 1.12
  diff -u -r1.11 -r1.12
  --- OptionConverter.cs        31 Jan 2005 22:56:49 -0000      1.11
  +++ OptionConverter.cs        4 Feb 2005 03:05:01 -0000       1.12
  @@ -275,35 +275,37 @@
                                return txt;
                        }
   
  -                     if (target.IsEnum)
  +                     // First lets try to find a type converter
  +                     IConvertFrom typeConverter = 
ConverterRegistry.GetConvertFrom(target);
  +                     if (typeConverter != null && 
typeConverter.CanConvertFrom(typeof(string)))
                        {
  -                             // Target type is an enum.
  -
  -                             // Use the Enum.Parse(EnumType, string) method 
to get the enum value
  -                             return ParseEnum(target, txt, true);
  +                             // Found appropriate converter
  +                             return typeConverter.ConvertFrom(txt);
                        }
                        else
                        {
  -                             // We essentially make a guess that to convert 
from a string
  -                             // to an arbitrary type T there will be a 
static method defined on type T called Parse
  -                             // that will take an argument of type string. 
i.e. T.Parse(string)->T we call this
  -                             // method to convert the string to the type 
required by the property.
  -                             System.Reflection.MethodInfo meth = 
target.GetMethod("Parse", new Type[] {typeof(string)});
  -                             if (meth != null)
  +                             if (target.IsEnum)
                                {
  -                                     // Call the Parse method
  -                                     return meth.Invoke(null, 
BindingFlags.InvokeMethod, null, new object[] {txt}, 
CultureInfo.InvariantCulture);
  +                                     // Target type is an enum.
  +
  +                                     // Use the Enum.Parse(EnumType, string) 
method to get the enum value
  +                                     return ParseEnum(target, txt, true);
                                }
                                else
                                {
  -                                     // Ok no Parse() method found.
  -
  -                                     // Lets try to find a type converter
  -                                     IConvertFrom typeConverter = 
ConverterRegistry.GetConvertFrom(target);
  -                                     if (typeConverter != null && 
typeConverter.CanConvertFrom(typeof(string)))
  +                                     // We essentially make a guess that to 
convert from a string
  +                                     // to an arbitrary type T there will be 
a static method defined on type T called Parse
  +                                     // that will take an argument of type 
string. i.e. T.Parse(string)->T we call this
  +                                     // method to convert the string to the 
type required by the property.
  +                                     System.Reflection.MethodInfo meth = 
target.GetMethod("Parse", new Type[] {typeof(string)});
  +                                     if (meth != null)
  +                                     {
  +                                             // Call the Parse method
  +                                             return meth.Invoke(null, 
BindingFlags.InvokeMethod, null, new object[] {txt}, 
CultureInfo.InvariantCulture);
  +                                     }
  +                                     else
                                        {
  -                                             // Found appropriate converter
  -                                             return 
typeConverter.ConvertFrom(txt);
  +                                             // No Parse() method found.
                                        }
                                }
                        }
  
  
  

Reply via email to