~ifolder-dev/simias/trunk-packaging

« back to all changes in this revision

Viewing changes to src/core/Simias.log4net/src/Util/.svn/text-base/OptionConverter.cs.svn-base

  • Committer: Jorge O. Castro
  • Date: 2007-12-03 06:56:46 UTC
  • Revision ID: jorge@ubuntu.com-20071203065646-mupcnjcwgm5mnhyt
* Remove a bunch of .svn directories we no longer need.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#region Copyright & License
2
 
//
3
 
// Copyright 2001-2005 The Apache Software Foundation
4
 
//
5
 
// Licensed under the Apache License, Version 2.0 (the "License");
6
 
// you may not use this file except in compliance with the License.
7
 
// You may obtain a copy of the License at
8
 
//
9
 
// http://www.apache.org/licenses/LICENSE-2.0
10
 
//
11
 
// Unless required by applicable law or agreed to in writing, software
12
 
// distributed under the License is distributed on an "AS IS" BASIS,
13
 
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
 
// See the License for the specific language governing permissions and
15
 
// limitations under the License.
16
 
//
17
 
#endregion
18
 
 
19
 
using System;
20
 
using System.Collections;
21
 
using System.Globalization;
22
 
using System.Reflection;
23
 
using System.Text;
24
 
 
25
 
using log4net.Core;
26
 
using log4net.Util.TypeConverters;
27
 
 
28
 
namespace log4net.Util
29
 
{
30
 
        /// <summary>
31
 
        /// A convenience class to convert property values to specific types.
32
 
        /// </summary>
33
 
        /// <remarks>
34
 
        /// <para>
35
 
        /// Utility functions for converting types and parsing values.
36
 
        /// </para>
37
 
        /// </remarks>
38
 
        /// <author>Nicko Cadell</author>
39
 
        /// <author>Gert Driesen</author>
40
 
        public sealed class OptionConverter
41
 
        {
42
 
                #region Private Instance Constructors
43
 
 
44
 
                /// <summary>
45
 
                /// Initializes a new instance of the <see cref="OptionConverter" /> class. 
46
 
                /// </summary>
47
 
                /// <remarks>
48
 
                /// <para>
49
 
                /// Uses a private access modifier to prevent instantiation of this class.
50
 
                /// </para>
51
 
                /// </remarks>
52
 
                private OptionConverter()
53
 
                {
54
 
                }
55
 
 
56
 
                #endregion Private Instance Constructors
57
 
 
58
 
                #region Public Static Methods
59
 
 
60
 
//              /// <summary>
61
 
//              /// Concatenates two string arrays.
62
 
//              /// </summary>
63
 
//              /// <param name="l">Left array.</param>
64
 
//              /// <param name="r">Right array.</param>
65
 
//              /// <returns>Array containing both left and right arrays.</returns>
66
 
//              public static string[] ConcatenateArrays(string[] l, string[] r) 
67
 
//              {
68
 
//                      return (string[])ConcatenateArrays(l, r);
69
 
//              }
70
 
 
71
 
//              /// <summary>
72
 
//              /// Concatenates two arrays.
73
 
//              /// </summary>
74
 
//              /// <param name="l">Left array</param>
75
 
//              /// <param name="r">Right array</param>
76
 
//              /// <returns>Array containing both left and right arrays.</returns>
77
 
//              public static Array ConcatenateArrays(Array l, Array r) 
78
 
//              {
79
 
//                      if (l == null)
80
 
//                      {
81
 
//                              throw new ArgumentNullException("l");
82
 
//                      }
83
 
//                      if (r == null)
84
 
//                      {
85
 
//                              throw new ArgumentNullException("r");
86
 
//                      }
87
 
//
88
 
//                      int len = l.Length + r.Length;
89
 
//                      Array a = Array.CreateInstance(l.GetType(), len);
90
 
//
91
 
//                      Array.Copy(l, 0, a, 0, l.Length);
92
 
//                      Array.Copy(r, 0, a, l.Length, r.Length);
93
 
//
94
 
//                      return a;
95
 
//              }
96
 
  
97
 
//              /// <summary>
98
 
//              /// Converts string escape characters back to their correct values.
99
 
//              /// </summary>
100
 
//              /// <param name="s">String to convert.</param>
101
 
//              /// <returns>Converted result.</returns>
102
 
//              public static string ConvertSpecialChars(string s) 
103
 
//              {
104
 
//                      if (s == null)
105
 
//                      {
106
 
//                              throw new ArgumentNullException("s");
107
 
//                      }
108
 
//                      char c;
109
 
//                      int len = s.Length;
110
 
//                      StringBuilder buf = new StringBuilder(len);
111
 
//      
112
 
//                      int i = 0;
113
 
//                      while(i < len) 
114
 
//                      {
115
 
//                              c = s[i++];
116
 
//                              if (c == '\\') 
117
 
//                              {
118
 
//                                      c =  s[i++];
119
 
//                                      if (c == 'n')     c = '\n';
120
 
//                                      else if (c == 'r') c = '\r';
121
 
//                                      else if (c == 't') c = '\t';
122
 
//                                      else if (c == 'f') c = '\f';
123
 
//                                      else if (c == '\b') c = '\b';                                   
124
 
//                                      else if (c == '\"') c = '\"';                           
125
 
//                                      else if (c == '\'') c = '\'';                   
126
 
//                                      else if (c == '\\') c = '\\';                   
127
 
//                              }
128
 
//                              buf.Append(c);    
129
 
//                      }
130
 
//                      return buf.ToString();
131
 
//              }
132
 
 
133
 
                /// <summary>
134
 
                /// Converts a string to a <see cref="bool" /> value.
135
 
                /// </summary>
136
 
                /// <param name="argValue">String to convert.</param>
137
 
                /// <param name="defaultValue">The default value.</param>
138
 
                /// <returns>The <see cref="bool" /> value of <paramref name="argValue" />.</returns>
139
 
                /// <remarks>
140
 
                /// <para>
141
 
                /// If <paramref name="argValue"/> is "true", then <c>true</c> is returned. 
142
 
                /// If <paramref name="argValue"/> is "false", then <c>false</c> is returned. 
143
 
                /// Otherwise, <paramref name="defaultValue"/> is returned.
144
 
                /// </para>
145
 
                /// </remarks>
146
 
                public static bool ToBoolean(string argValue, bool defaultValue) 
147
 
                {
148
 
                        if (argValue != null && argValue.Length > 0)
149
 
                        {
150
 
                                try
151
 
                                {
152
 
                                        return bool.Parse(argValue);
153
 
                                }
154
 
                                catch(Exception e)
155
 
                                {
156
 
                                        LogLog.Error("OptionConverter: [" + argValue + "] is not in proper bool form.", e);
157
 
                                }
158
 
                        }
159
 
                        return defaultValue;
160
 
                }
161
 
 
162
 
//              /// <summary>
163
 
//              /// Converts a string to an integer.
164
 
//              /// </summary>
165
 
//              /// <param name="argValue">String to convert.</param>
166
 
//              /// <param name="defaultValue">The default value.</param>
167
 
//              /// <returns>The <see cref="int" /> value of <paramref name="argValue" />.</returns>
168
 
//              /// <remarks>
169
 
//              /// <para>
170
 
//              /// <paramref name="defaultValue"/> is returned when <paramref name="argValue"/>
171
 
//              /// cannot be converted to a <see cref="int" /> value.
172
 
//              /// </para>
173
 
//              /// </remarks>
174
 
//              public static int ToInt(string argValue, int defaultValue) 
175
 
//              {
176
 
//                      if (argValue != null) 
177
 
//                      {
178
 
//                              string s = argValue.Trim();
179
 
//                              try 
180
 
//                              {
181
 
//                                      return int.Parse(s, NumberFormatInfo.InvariantInfo);
182
 
//                              }
183
 
//                              catch (Exception e) 
184
 
//                              {
185
 
//                                      LogLog.Error("OptionConverter: [" + s + "] is not in proper int form.", e);
186
 
//                              }
187
 
//                      }
188
 
//                      return defaultValue;
189
 
//              }
190
 
 
191
 
                /// <summary>
192
 
                /// Parses a file size into a number.
193
 
                /// </summary>
194
 
                /// <param name="argValue">String to parse.</param>
195
 
                /// <param name="defaultValue">The default value.</param>
196
 
                /// <returns>The <see cref="long" /> value of <paramref name="argValue" />.</returns>
197
 
                /// <remarks>
198
 
                /// <para>
199
 
                /// Parses a file size of the form: number[KB|MB|GB] into a
200
 
                /// long value. It is scaled with the appropriate multiplier.
201
 
                /// </para>
202
 
                /// <para>
203
 
                /// <paramref name="defaultValue"/> is returned when <paramref name="argValue"/>
204
 
                /// cannot be converted to a <see cref="long" /> value.
205
 
                /// </para>
206
 
                /// </remarks>
207
 
                public static long ToFileSize(string argValue, long defaultValue) 
208
 
                {
209
 
                        if (argValue == null)
210
 
                        {
211
 
                                return defaultValue;
212
 
                        }
213
 
        
214
 
                        string s = argValue.Trim().ToUpper(CultureInfo.InvariantCulture);
215
 
                        long multiplier = 1;
216
 
                        int index;
217
 
        
218
 
                        if ((index = s.IndexOf("KB")) != -1) 
219
 
                        {         
220
 
                                multiplier = 1024;
221
 
                                s = s.Substring(0, index);
222
 
                        }
223
 
                        else if ((index = s.IndexOf("MB")) != -1) 
224
 
                        {
225
 
                                multiplier = 1024 * 1024;
226
 
                                s = s.Substring(0, index);
227
 
                        }
228
 
                        else if ((index = s.IndexOf("GB")) != -1) 
229
 
                        {
230
 
                                multiplier = 1024 * 1024 * 1024;
231
 
                                s = s.Substring(0, index);
232
 
                        }       
233
 
                        if (s != null) 
234
 
                        {
235
 
                                // Try again to remove whitespace between the number and the size specifier
236
 
                                s = s.Trim();
237
 
                                
238
 
                                long longVal;
239
 
                                if (SystemInfo.TryParse(s, out longVal))
240
 
                                {
241
 
                                        return longVal * multiplier;
242
 
                                }
243
 
                                else
244
 
                                {
245
 
                                        LogLog.Error("OptionConverter: ["+ s +"] is not in the correct file size syntax.");
246
 
                                }
247
 
                        }
248
 
                        return defaultValue;
249
 
                }
250
 
 
251
 
                /// <summary>
252
 
                /// Converts a string to an object.
253
 
                /// </summary>
254
 
                /// <param name="target">The target type to convert to.</param>
255
 
                /// <param name="txt">The string to convert to an object.</param>
256
 
                /// <returns>
257
 
                /// The object converted from a string or <c>null</c> when the 
258
 
                /// conversion failed.
259
 
                /// </returns>
260
 
                /// <remarks>
261
 
                /// <para>
262
 
                /// Converts a string to an object. Uses the converter registry to try
263
 
                /// to convert the string value into the specified target type.
264
 
                /// </para>
265
 
                /// </remarks>
266
 
                public static object ConvertStringTo(Type target, string txt)
267
 
                {
268
 
                        if (target == null)
269
 
                        {
270
 
                                throw new ArgumentNullException("target");
271
 
                        }
272
 
 
273
 
                        // If we want a string we already have the correct type
274
 
                        if (typeof(string) == target || typeof(object) == target)
275
 
                        {
276
 
                                return txt;
277
 
                        }
278
 
 
279
 
                        // First lets try to find a type converter
280
 
                        IConvertFrom typeConverter = ConverterRegistry.GetConvertFrom(target);
281
 
                        if (typeConverter != null && typeConverter.CanConvertFrom(typeof(string)))
282
 
                        {
283
 
                                // Found appropriate converter
284
 
                                return typeConverter.ConvertFrom(txt);
285
 
                        }
286
 
                        else
287
 
                        {
288
 
                                if (target.IsEnum)
289
 
                                {
290
 
                                        // Target type is an enum.
291
 
 
292
 
                                        // Use the Enum.Parse(EnumType, string) method to get the enum value
293
 
                                        return ParseEnum(target, txt, true);
294
 
                                }
295
 
                                else
296
 
                                {
297
 
                                        // We essentially make a guess that to convert from a string
298
 
                                        // to an arbitrary type T there will be a static method defined on type T called Parse
299
 
                                        // that will take an argument of type string. i.e. T.Parse(string)->T we call this
300
 
                                        // method to convert the string to the type required by the property.
301
 
                                        System.Reflection.MethodInfo meth = target.GetMethod("Parse", new Type[] {typeof(string)});
302
 
                                        if (meth != null)
303
 
                                        {
304
 
                                                // Call the Parse method
305
 
                                                return meth.Invoke(null, BindingFlags.InvokeMethod, null, new object[] {txt}, CultureInfo.InvariantCulture);
306
 
                                        }
307
 
                                        else
308
 
                                        {
309
 
                                                // No Parse() method found.
310
 
                                        }
311
 
                                }
312
 
                        }
313
 
                        return null;
314
 
                }
315
 
 
316
 
//              /// <summary>
317
 
//              /// Looks up the <see cref="IConvertFrom"/> for the target type.
318
 
//              /// </summary>
319
 
//              /// <param name="target">The type to lookup the converter for.</param>
320
 
//              /// <returns>The converter for the specified type.</returns>
321
 
//              public static IConvertFrom GetTypeConverter(Type target)
322
 
//              {
323
 
//                      IConvertFrom converter = ConverterRegistry.GetConverter(target);
324
 
//                      if (converter == null)
325
 
//                      {
326
 
//                              throw new InvalidOperationException("No type converter defined for [" + target + "]");
327
 
//                      }
328
 
//                      return converter;
329
 
//              }
330
 
 
331
 
                /// <summary>
332
 
                /// Checks if there is an appropriate type conversion from the source type to the target type.
333
 
                /// </summary>
334
 
                /// <param name="sourceType">The type to convert from.</param>
335
 
                /// <param name="targetType">The type to convert to.</param>
336
 
                /// <returns><c>true</c> if there is a conversion from the source type to the target type.</returns>
337
 
                /// <remarks>
338
 
                /// Checks if there is an appropriate type conversion from the source type to the target type.
339
 
                /// <para>
340
 
                /// </para>
341
 
                /// </remarks>
342
 
                public static bool CanConvertTypeTo(Type sourceType, Type targetType)
343
 
                {
344
 
                        if (sourceType == null || targetType == null)
345
 
                        {
346
 
                                return false;
347
 
                        }
348
 
 
349
 
                        // Check if we can assign directly from the source type to the target type
350
 
                        if (targetType.IsAssignableFrom(sourceType))
351
 
                        {
352
 
                                return true;
353
 
                        }
354
 
 
355
 
                        // Look for a To converter
356
 
                        IConvertTo tcSource = ConverterRegistry.GetConvertTo(sourceType, targetType);
357
 
                        if (tcSource != null)
358
 
                        {
359
 
                                if (tcSource.CanConvertTo(targetType))
360
 
                                {
361
 
                                        return true;
362
 
                                }
363
 
                        }
364
 
 
365
 
                        // Look for a From converter
366
 
                        IConvertFrom tcTarget = ConverterRegistry.GetConvertFrom(targetType);
367
 
                        if (tcTarget != null)
368
 
                        {
369
 
                                if (tcTarget.CanConvertFrom(sourceType))
370
 
                                {
371
 
                                        return true;
372
 
                                }
373
 
                        }
374
 
 
375
 
                        return false;
376
 
                }
377
 
 
378
 
                /// <summary>
379
 
                /// Converts an object to the target type.
380
 
                /// </summary>
381
 
                /// <param name="sourceInstance">The object to convert to the target type.</param>
382
 
                /// <param name="targetType">The type to convert to.</param>
383
 
                /// <returns>The converted object.</returns>
384
 
                /// <remarks>
385
 
                /// <para>
386
 
                /// Converts an object to the target type.
387
 
                /// </para>
388
 
                /// </remarks>
389
 
                public static object ConvertTypeTo(object sourceInstance, Type targetType)
390
 
                {
391
 
                        Type sourceType = sourceInstance.GetType();
392
 
 
393
 
                        // Check if we can assign directly from the source type to the target type
394
 
                        if (targetType.IsAssignableFrom(sourceType))
395
 
                        {
396
 
                                return sourceInstance;
397
 
                        }
398
 
 
399
 
                        // Look for a TO converter
400
 
                        IConvertTo tcSource = ConverterRegistry.GetConvertTo(sourceType, targetType);
401
 
                        if (tcSource != null)
402
 
                        {
403
 
                                if (tcSource.CanConvertTo(targetType))
404
 
                                {
405
 
                                        return tcSource.ConvertTo(sourceInstance, targetType);
406
 
                                }
407
 
                        }
408
 
 
409
 
                        // Look for a FROM converter
410
 
                        IConvertFrom tcTarget = ConverterRegistry.GetConvertFrom(targetType);
411
 
                        if (tcTarget != null)
412
 
                        {
413
 
                                if (tcTarget.CanConvertFrom(sourceType))
414
 
                                {
415
 
                                        return tcTarget.ConvertFrom(sourceInstance);
416
 
                                }
417
 
                        }
418
 
 
419
 
                        throw new ArgumentException("Cannot convert source object [" + sourceInstance.ToString() + "] to target type [" + targetType.Name + "]", "sourceInstance");
420
 
                }
421
 
 
422
 
//              /// <summary>
423
 
//              /// Finds the value corresponding to <paramref name="key"/> in 
424
 
//              /// <paramref name="props"/> and then perform variable substitution 
425
 
//              /// on the found value.
426
 
//              /// </summary>
427
 
//              /// <param name="key">The key to lookup.</param>
428
 
//              /// <param name="props">The association to use for lookups.</param>
429
 
//              /// <returns>The substituted result.</returns>
430
 
//              public static string FindAndSubst(string key, System.Collections.IDictionary props) 
431
 
//              {
432
 
//                      if (props == null)
433
 
//                      {
434
 
//                              throw new ArgumentNullException("props");
435
 
//                      }
436
 
//
437
 
//                      string v = props[key] as string;
438
 
//                      if (v == null) 
439
 
//                      {
440
 
//                              return null;      
441
 
//                      }
442
 
//      
443
 
//                      try 
444
 
//                      {
445
 
//                              return SubstituteVariables(v, props);
446
 
//                      } 
447
 
//                      catch(Exception e) 
448
 
//                      {
449
 
//                              LogLog.Error("OptionConverter: Bad option value [" + v + "].", e);
450
 
//                              return v;
451
 
//                      }       
452
 
//              }
453
 
 
454
 
                /// <summary>
455
 
                /// Instantiates an object given a class name.
456
 
                /// </summary>
457
 
                /// <param name="className">The fully qualified class name of the object to instantiate.</param>
458
 
                /// <param name="superClass">The class to which the new object should belong.</param>
459
 
                /// <param name="defaultValue">The object to return in case of non-fulfillment.</param>
460
 
                /// <returns>
461
 
                /// An instance of the <paramref name="className"/> or <paramref name="defaultValue"/>
462
 
                /// if the object could not be instantiated.
463
 
                /// </returns>
464
 
                /// <remarks>
465
 
                /// <para>
466
 
                /// Checks that the <paramref name="className"/> is a subclass of
467
 
                /// <paramref name="superClass"/>. If that test fails or the object could
468
 
                /// not be instantiated, then <paramref name="defaultValue"/> is returned.
469
 
                /// </para>
470
 
                /// </remarks>
471
 
                public static object InstantiateByClassName(string className, Type superClass, object defaultValue) 
472
 
                {
473
 
                        if (className != null) 
474
 
                        {
475
 
                                try 
476
 
                                {
477
 
                                        Type classObj = SystemInfo.GetTypeFromString(className, true, true);
478
 
                                        if (!superClass.IsAssignableFrom(classObj)) 
479
 
                                        {
480
 
                                                LogLog.Error("OptionConverter: A [" + className + "] object is not assignable to a [" + superClass.FullName + "] variable.");
481
 
                                                return defaultValue;      
482
 
                                        }
483
 
                                        return Activator.CreateInstance(classObj);
484
 
                                }
485
 
                                catch (Exception e) 
486
 
                                {
487
 
                                        LogLog.Error("OptionConverter: Could not instantiate class [" + className + "].", e);
488
 
                                }
489
 
                        }
490
 
                        return defaultValue;    
491
 
                }
492
 
 
493
 
                /// <summary>
494
 
                /// Performs variable substitution in string <paramref name="val"/> from the 
495
 
                /// values of keys found in <paramref name="props"/>.
496
 
                /// </summary>
497
 
                /// <param name="value">The string on which variable substitution is performed.</param>
498
 
                /// <param name="props">The dictionary to use to lookup variables.</param>
499
 
                /// <returns>The result of the substitutions.</returns>
500
 
                /// <remarks>
501
 
                /// <para>
502
 
                /// The variable substitution delimiters are <b>${</b> and <b>}</b>.
503
 
                /// </para>
504
 
                /// <para>
505
 
                /// For example, if props contains <c>key=value</c>, then the call
506
 
                /// </para>
507
 
                /// <para>
508
 
                /// <code lang="C#">
509
 
                /// string s = OptionConverter.SubstituteVariables("Value of key is ${key}.");
510
 
                /// </code>
511
 
                /// </para>
512
 
                /// <para>
513
 
                /// will set the variable <c>s</c> to "Value of key is value.".
514
 
                /// </para>
515
 
                /// <para>
516
 
                /// If no value could be found for the specified key, then substitution 
517
 
                /// defaults to an empty string.
518
 
                /// </para>
519
 
                /// <para>
520
 
                /// For example, if system properties contains no value for the key
521
 
                /// "nonExistentKey", then the call
522
 
                /// </para>
523
 
                /// <para>
524
 
                /// <code lang="C#">
525
 
                /// string s = OptionConverter.SubstituteVariables("Value of nonExistentKey is [${nonExistentKey}]");
526
 
                /// </code>
527
 
                /// </para>
528
 
                /// <para>
529
 
                /// will set <s>s</s> to "Value of nonExistentKey is []".        
530
 
                /// </para>
531
 
                /// <para>
532
 
                /// An Exception is thrown if <paramref name="value"/> contains a start 
533
 
                /// delimiter "${" which is not balanced by a stop delimiter "}". 
534
 
                /// </para>
535
 
                /// </remarks>
536
 
                public static string SubstituteVariables(string value, System.Collections.IDictionary props) 
537
 
                {
538
 
                        StringBuilder buf = new StringBuilder();
539
 
 
540
 
                        int i = 0;
541
 
                        int j, k;
542
 
        
543
 
                        while(true) 
544
 
                        {
545
 
                                j = value.IndexOf(DELIM_START, i);
546
 
                                if (j == -1) 
547
 
                                {
548
 
                                        if (i == 0)
549
 
                                        {
550
 
                                                return value;
551
 
                                        }
552
 
                                        else 
553
 
                                        {
554
 
                                                buf.Append(value.Substring(i, value.Length - i));
555
 
                                                return buf.ToString();
556
 
                                        }
557
 
                                }
558
 
                                else 
559
 
                                {
560
 
                                        buf.Append(value.Substring(i, j - i));
561
 
                                        k = value.IndexOf(DELIM_STOP, j);
562
 
                                        if (k == -1) 
563
 
                                        {
564
 
                                                throw new LogException("[" + value + "] has no closing brace. Opening brace at position [" + j + "]");
565
 
                                        }
566
 
                                        else 
567
 
                                        {
568
 
                                                j += DELIM_START_LEN;
569
 
                                                string key = value.Substring(j, k - j);
570
 
 
571
 
                                                string replacement = props[key] as string;
572
 
 
573
 
                                                if (replacement != null) 
574
 
                                                {
575
 
                                                        buf.Append(replacement);
576
 
                                                }
577
 
                                                i = k + DELIM_STOP_LEN;         
578
 
                                        }
579
 
                                }
580
 
                        }
581
 
                }
582
 
 
583
 
                #endregion Public Static Methods
584
 
 
585
 
                #region Private Static Methods
586
 
 
587
 
                /// <summary>
588
 
                /// Converts the string representation of the name or numeric value of one or 
589
 
                /// more enumerated constants to an equivalent enumerated object.
590
 
                /// </summary>
591
 
                /// <param name="enumType">The type to convert to.</param>
592
 
                /// <param name="value">The enum string value.</param>
593
 
                /// <param name="ignoreCase">If <c>true</c>, ignore case; otherwise, regard case.</param>
594
 
                /// <returns>An object of type <paramref name="enumType" /> whose value is represented by <paramref name="value" />.</returns>
595
 
                private static object ParseEnum(System.Type enumType, string value, bool ignoreCase) 
596
 
                {
597
 
#if !NETCF
598
 
                        return Enum.Parse(enumType, value, ignoreCase);
599
 
#else
600
 
                        FieldInfo[] fields = enumType.GetFields(BindingFlags.Public | BindingFlags.Static);
601
 
 
602
 
                        string[] names = value.Split(new char[] {','});
603
 
                        for (int i = 0; i < names.Length; ++i) 
604
 
                        {
605
 
                                names[i] = names [i].Trim();
606
 
                        }
607
 
 
608
 
                        long retVal = 0;
609
 
 
610
 
                        try 
611
 
                        {
612
 
                                // Attempt to convert to numeric type
613
 
                                return Enum.ToObject(enumType, Convert.ChangeType(value, typeof(long), CultureInfo.InvariantCulture));
614
 
                        } 
615
 
                        catch {}
616
 
 
617
 
                        foreach (string name in names) 
618
 
                        {
619
 
                                bool found = false;
620
 
                                foreach(FieldInfo field in fields) 
621
 
                                {
622
 
                                        if (String.Compare(name, field.Name, ignoreCase) == 0) 
623
 
                                        {
624
 
                                                retVal |= ((IConvertible) field.GetValue(null)).ToInt64(CultureInfo.InvariantCulture);
625
 
                                                found = true;
626
 
                                                break;
627
 
                                        }
628
 
                                }
629
 
                                if (!found) 
630
 
                                {
631
 
                                        throw new ArgumentException("Failed to lookup member [" + name + "] from Enum type [" + enumType.Name + "]");
632
 
                                }
633
 
                        }
634
 
                        return Enum.ToObject(enumType, retVal);
635
 
#endif
636
 
                }               
637
 
 
638
 
                #endregion Private Static Methods
639
 
 
640
 
                #region Private Static Fields
641
 
 
642
 
                private const string DELIM_START = "${";
643
 
                private const char   DELIM_STOP  = '}';
644
 
                private const int DELIM_START_LEN = 2;
645
 
                private const int DELIM_STOP_LEN  = 1;
646
 
 
647
 
                #endregion Private Static Fields
648
 
        }
649
 
}