0
|
1 using System;
|
|
2
|
|
3 using BLToolkit.Mapping;
|
|
4
|
|
5 namespace BLTgen
|
|
6 {
|
|
7 public class StringListMapper : MapDataSourceBase
|
|
8 {
|
|
9 string[] _list;
|
|
10
|
|
11 public StringListMapper(string[] list)
|
|
12 {
|
|
13 _list = list;
|
|
14 }
|
|
15
|
|
16 #region IMapDataSource
|
|
17
|
|
18 public override int Count
|
|
19 {
|
|
20 get { return _list.Length; }
|
|
21 }
|
|
22
|
|
23 public override Type GetFieldType(int index)
|
|
24 {
|
|
25 return (index > 0 && index < _list.Length)? typeof(string): null;
|
|
26 }
|
|
27
|
|
28 public override string GetName(int index)
|
|
29 {
|
|
30 return GetNameOrValue(_list[index], true);
|
|
31 }
|
|
32
|
|
33 public override int GetOrdinal(string name)
|
|
34 {
|
|
35 throw new InvalidOperationException("IMapDataSource.GetOrdinal(string)");
|
|
36 }
|
|
37
|
|
38 public override object GetValue(object o, int index)
|
|
39 {
|
|
40 return GetNameOrValue(_list[index], false);
|
|
41 }
|
|
42
|
|
43 public override object GetValue(object o, string name)
|
|
44 {
|
|
45 throw new InvalidOperationException("IMapDataSource.GetValue(object, string)");
|
|
46 }
|
|
47
|
|
48 #endregion
|
|
49
|
|
50 #region Implementation
|
|
51
|
|
52 private static string GetNameOrValue(string str, bool name)
|
|
53 {
|
|
54 if (str.StartsWith("\"") && str.EndsWith("\""))
|
|
55 str = str.Substring(1, str.Length - 2);
|
|
56
|
|
57 // Option
|
|
58 //
|
|
59 if (str.StartsWith("-") || str.StartsWith("/"))
|
|
60 {
|
|
61 int colon = str.IndexOfAny(new char[] { ':', '=' }, 1);
|
|
62 if (colon > 0)
|
|
63 return name ? str.Substring(1, colon - 1) : str.Substring(colon + 1);
|
|
64
|
|
65 return name ? str.Substring(1) : string.Empty;
|
|
66 }
|
|
67
|
|
68 // Default parameter
|
|
69 //
|
|
70 return name ? string.Empty : str;
|
|
71 }
|
|
72
|
|
73 #endregion
|
|
74 }
|
|
75 }
|