comparison Source/Common/CompatibilitySL.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.Globalization;
3 using System.Linq;
4 using System.Runtime.InteropServices;
5 using System.Runtime.Serialization;
6 using System.Text;
7 using JetBrains.Annotations;
8
9 namespace System
10 {
11 [AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Enum | AttributeTargets.Delegate, Inherited = false)]
12 class SerializableAttribute : Attribute
13 {
14 }
15
16 [ComVisible(true)]
17 interface ICloneable
18 {
19 object Clone();
20 }
21
22 namespace Collections
23 {
24 public class Comparer : IComparer
25 {
26 readonly CompareInfo _compareInfo;
27
28 public static readonly Comparer Default = new Comparer(CultureInfo.CurrentCulture);
29 public static readonly Comparer DefaultInvariant = new Comparer(CultureInfo.InvariantCulture);
30
31 private const String CompareInfoName = "CompareInfo";
32
33 public Comparer([NotNull] CultureInfo culture)
34 {
35 if (culture == null) throw new ArgumentNullException("culture");
36 _compareInfo = culture.CompareInfo;
37 }
38
39 public int Compare(object a, object b)
40 {
41 if (a == b) return 0;
42 if (a == null) return -1;
43 if (b == null) return 1;
44
45 if (_compareInfo != null) {
46 var sa = a as String;
47 var sb = b as String;
48
49 if (sa != null && sb != null)
50 return _compareInfo.Compare(sa, sb);
51 }
52
53 var ia = a as IComparable;
54
55 if (ia != null)
56 return ia.CompareTo(b);
57
58 throw new ArgumentException("Object should implement IComparable interface.");
59 }
60 }
61
62 namespace Generic
63 {
64 public static class Extensions
65 {
66 public static List<TOutput> ConvertAll<T,TOutput>(this List<T> input, Converter<T,TOutput> converter)
67 {
68 var list = new List<TOutput>(input.Count);
69
70 list.AddRange(from T t in input select converter(t));
71
72 return list;
73 }
74
75 public static bool Exists<T>(this List<T> list, Predicate<T> match)
76 {
77 return list.Any(item => match(item));
78 }
79 }
80 }
81 }
82
83 namespace ComponentModel
84 {
85 [AttributeUsage(AttributeTargets.Property | AttributeTargets.Event | AttributeTargets.Class | AttributeTargets.Method)]
86 public class DisplayNameAttribute : Attribute
87 {
88 public static readonly DisplayNameAttribute Default = new DisplayNameAttribute();
89
90 public DisplayNameAttribute()
91 {
92 DisplayName = string.Empty;
93 }
94
95 public DisplayNameAttribute(string displayName)
96 {
97 DisplayName = displayName;
98 }
99
100 public string DisplayName{ get; set; }
101 }
102 }
103
104 namespace Data
105 {
106 public interface IDataRecord
107 {
108 int FieldCount { get; }
109 object this[int i] { get; }
110 object this[string name] { get; }
111
112 string GetName (int i);
113 string GetDataTypeName(int i);
114 Type GetFieldType (int i);
115 object GetValue (int i);
116 int GetValues (object[] values);
117 int GetOrdinal (string name);
118 bool GetBoolean (int i);
119 byte GetByte (int i);
120 long GetBytes (int i, long fieldOffset, byte[] buffer, int bufferoffset, int length);
121 char GetChar (int i);
122 long GetChars (int i, long fieldoffset, char[] buffer, int bufferoffset, int length);
123 Guid GetGuid (int i);
124 short GetInt16 (int i);
125 int GetInt32 (int i);
126 long GetInt64 (int i);
127 float GetFloat (int i);
128 double GetDouble (int i);
129 string GetString (int i);
130 decimal GetDecimal (int i);
131 DateTime GetDateTime (int i);
132 IDataReader GetData (int i);
133 bool IsDBNull (int i);
134 }
135
136 public interface IDataReader : IDisposable, IDataRecord
137 {
138 int Depth { get; }
139 bool IsClosed { get; }
140 int RecordsAffected { get; }
141 void Close();
142 //DataTable GetSchemaTable();
143 bool NextResult();
144 bool Read();
145 }
146
147 public enum SqlDbType
148 {
149 BigInt = 0,
150 Binary = 1,
151 Bit = 2,
152 Char = 3,
153 DateTime = 4,
154 Decimal = 5,
155 Float = 6,
156 Image = 7,
157 Int = 8,
158 Money = 9,
159 NChar = 10,
160 NText = 11,
161 NVarChar = 12,
162 Real = 13,
163 UniqueIdentifier = 14,
164 SmallDateTime = 15,
165 SmallInt = 16,
166 SmallMoney = 17,
167 Text = 18,
168 Timestamp = 19,
169 TinyInt = 20,
170 VarBinary = 21,
171 VarChar = 22,
172 Variant = 23,
173 Xml = 25,
174 Udt = 29,
175 Structured = 30,
176 Date = 31,
177 Time = 32,
178 DateTime2 = 33,
179 DateTimeOffset = 34,
180 }
181
182 public enum DbType
183 {
184 AnsiString = 0,
185 Binary = 1,
186 Byte = 2,
187 Boolean = 3,
188 Currency = 4,
189 Date = 5,
190 DateTime = 6,
191 Decimal = 7,
192 Double = 8,
193 Guid = 9,
194 Int16 = 10,
195 Int32 = 11,
196 Int64 = 12,
197 Object = 13,
198 SByte = 14,
199 Single = 15,
200 String = 16,
201 Time = 17,
202 UInt16 = 18,
203 UInt32 = 19,
204 UInt64 = 20,
205 VarNumeric = 21,
206 AnsiStringFixedLength = 22,
207 StringFixedLength = 23,
208 Xml = 25,
209 DateTime2 = 26,
210 DateTimeOffset = 27,
211 }
212
213 namespace Linq
214 {
215 [DataContract]
216 [Serializable]
217 public sealed class Binary : IEquatable<Binary>
218 {
219 [DataMember(Name="Bytes")] byte[] _bytes;
220
221 int? _hashCode;
222
223 public Binary(byte[] value)
224 {
225 if (value == null)
226 throw new ArgumentNullException("value");
227
228 _bytes = new byte[value.Length];
229 Array.Copy(value, _bytes, value.Length);
230 ComputeHash();
231 }
232
233 public byte[] ToArray()
234 {
235 var copy = new byte[this._bytes.Length];
236 Array.Copy(_bytes, copy, copy.Length);
237 return copy;
238 }
239
240 public int Length
241 {
242 get { return _bytes.Length; }
243 }
244
245 public static implicit operator Binary(byte[] value)
246 {
247 return new Binary(value);
248 }
249
250 public bool Equals(Binary other)
251 {
252 return EqualsTo(other);
253 }
254
255 public static bool operator == (Binary binary1, Binary binary2)
256 {
257 if ((object)binary1 == (object)binary2) return true;
258 if ((object)binary1 == null && (object)binary2 == null) return true;
259 if ((object)binary1 == null || (object)binary2 == null) return false;
260
261 return binary1.EqualsTo(binary2);
262 }
263
264 public static bool operator !=(Binary binary1, Binary binary2)
265 {
266 if ((object)binary1 == (object)binary2) return false;
267 if ((object)binary1 == null && (object)binary2 == null) return false;
268 if ((object)binary1 == null || (object)binary2 == null) return true;
269
270 return !binary1.EqualsTo(binary2);
271 }
272
273 public override bool Equals(object obj)
274 {
275 return EqualsTo(obj as Binary);
276 }
277
278 public override int GetHashCode()
279 {
280 if (!_hashCode.HasValue)
281 ComputeHash();
282
283 return _hashCode.Value;
284 }
285
286 public override string ToString()
287 {
288 var sb = new StringBuilder();
289
290 sb.Append("\"");
291 sb.Append(Convert.ToBase64String(_bytes, 0, _bytes.Length));
292 sb.Append("\"");
293
294 return sb.ToString();
295 }
296
297 private bool EqualsTo(Binary binary)
298 {
299 if ((object)this == (object)binary) return true;
300 if ((object)binary == null) return false;
301 if (_bytes.Length != binary._bytes.Length) return false;
302 if (_hashCode != binary._hashCode) return false;
303
304 for (int i = 0; i < _bytes.Length; i++)
305 if (_bytes[i] != binary._bytes[i])
306 return false;
307
308 return true;
309 }
310
311 private void ComputeHash()
312 {
313 var s = 314;
314 const int t = 159;
315
316 _hashCode = 0;
317
318 for (var i = 0; i < _bytes.Length; i++)
319 {
320 _hashCode = _hashCode * s + _bytes[i];
321 s = s * t;
322 }
323 }
324 }
325 }
326
327 namespace SqlTypes
328 {
329 public interface INullable
330 {
331 bool IsNull { get; }
332 }
333 }
334 }
335 }