comparison Source/Common/NameOrIndexParameter.cs @ 0:f990fcb411a9

Копия текущей версии из github
author cin
date Thu, 27 Mar 2014 21:46:09 +0400
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:f990fcb411a9
1 using System;
2 using System.Linq;
3 using BLToolkit.Properties;
4
5 namespace BLToolkit.Common
6 {
7 /// <summary>
8 /// This argument adapter class allows either names (strings) or
9 /// indices (ints) to be passed to a function.
10 /// </summary>
11 [System.Diagnostics.DebuggerStepThrough]
12 public struct NameOrIndexParameter
13 {
14 public NameOrIndexParameter(string name)
15 {
16 if (null == name)
17 throw new ArgumentNullException("name");
18
19 if (name.Length == 0)
20 throw new ArgumentException(Resources.NameOrIndexParameter_BadName, "name");
21
22 _name = name;
23 _index = 0;
24 }
25
26 public NameOrIndexParameter(int index)
27 {
28 if (index < 0)
29 throw new ArgumentException(Resources.NameOrIndexParameter_BadIndex, "index");
30
31 _name = null;
32 _index = index;
33 }
34
35 public static implicit operator NameOrIndexParameter(string name)
36 {
37 return new NameOrIndexParameter(name);
38 }
39
40 public static implicit operator NameOrIndexParameter(int index)
41 {
42 return new NameOrIndexParameter(index);
43 }
44
45 #region Public properties
46
47 public bool ByName
48 {
49 get { return null != _name; }
50 }
51
52 private readonly string _name;
53 public string Name
54 {
55 get
56 {
57 if (null == _name)
58 throw new InvalidOperationException(
59 "This instance was initialized by index");
60
61 return _name;
62 }
63 }
64
65 private readonly int _index;
66 public int Index
67 {
68 get
69 {
70 if (null != _name)
71 throw new InvalidOperationException(
72 "This instance was initialized by name");
73
74 return _index;
75 }
76 }
77
78 #endregion
79
80 #region Static methods
81
82 public static NameOrIndexParameter[] FromStringArray(string[] names)
83 {
84 return names.Select(name => new NameOrIndexParameter(name)).ToArray();
85 }
86
87 public static NameOrIndexParameter[] FromIndexArray(int[] indices)
88 {
89 return indices.Select(index => new NameOrIndexParameter(index)).ToArray();
90 }
91
92 #endregion
93
94 #region System.Object members
95
96 public override bool Equals(object obj)
97 {
98 if (obj is NameOrIndexParameter)
99 {
100 var nip = (NameOrIndexParameter)obj;
101
102 if (null != _name && null != nip._name && _name == nip._name)
103 return true; // Same name
104
105 if (null == _name && null == nip._name && _index == nip._index)
106 return true; // Same index
107
108 return false;
109 }
110
111 if (obj is string)
112 {
113 var name = (string)obj;
114 return (null != _name && _name == name);
115 }
116
117 if (obj is int)
118 {
119 var index = (int)obj;
120 return (null == _name && _index == index);
121 }
122
123 return false;
124 }
125
126 public override int GetHashCode()
127 {
128 return (null != _name) ? _name.GetHashCode() : _index.GetHashCode();
129 }
130
131 public override string ToString()
132 {
133 return _name ?? "#" + _index;
134 }
135
136 #endregion
137
138 }
139 }