mirror of
https://github.com/esiur/esiur-dotnet.git
synced 2025-06-27 05:23:13 +00:00
1.5
This commit is contained in:
@ -32,7 +32,7 @@ using System.Reflection;
|
||||
|
||||
namespace Esyur.Data
|
||||
{
|
||||
public class AutoList<T, ST> : IEnumerable<T>
|
||||
public class AutoList<T, ST> : IEnumerable<T>, ICollection, ICollection<T>
|
||||
{
|
||||
|
||||
private readonly object syncRoot = new object();
|
||||
@ -239,6 +239,10 @@ namespace Esyur.Data
|
||||
get { return list.Count; }
|
||||
}
|
||||
|
||||
public bool IsSynchronized => (list as ICollection).IsSynchronized;
|
||||
|
||||
public bool IsReadOnly => throw new NotImplementedException();
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Check if an item exists in the list
|
||||
@ -282,5 +286,20 @@ namespace Esyur.Data
|
||||
{
|
||||
return ((IEnumerable<T>)list).GetEnumerator();
|
||||
}
|
||||
|
||||
public void CopyTo(Array array, int index)
|
||||
{
|
||||
(list as ICollection).CopyTo(array, index);
|
||||
}
|
||||
|
||||
public void CopyTo(T[] array, int arrayIndex)
|
||||
{
|
||||
list.CopyTo(array, arrayIndex);
|
||||
}
|
||||
|
||||
bool ICollection<T>.Remove(T item)
|
||||
{
|
||||
return list.Remove(item);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -43,13 +43,9 @@ namespace Esyur.Data
|
||||
{
|
||||
public static object CastConvert(object value, Type destinationType)
|
||||
{
|
||||
|
||||
if (value == null)
|
||||
return null;
|
||||
|
||||
//if (destinationType.IsArray && destinationType.GetElementType().IsArray)
|
||||
// Console.Beep();
|
||||
|
||||
var sourceType = value.GetType();
|
||||
|
||||
if (destinationType == sourceType)
|
||||
@ -69,22 +65,6 @@ namespace Esyur.Data
|
||||
for (var i = 0; i < rt.Length; i++)
|
||||
{
|
||||
rt.SetValue(CastConvert(v.GetValue(i), destinationType), i);
|
||||
|
||||
// try
|
||||
// {
|
||||
//#if NETSTANDARD
|
||||
// if (destinationType.GetTypeInfo().IsInstanceOfType(v.GetValue(i)))
|
||||
//#else
|
||||
// if (destinationType.IsInstanceOfType(v.GetValue(i)))
|
||||
//#endif
|
||||
// rt.SetValue(v.GetValue(i), i);
|
||||
// else
|
||||
// rt.SetValue(Convert.ChangeType(v.GetValue(i), destinationType), i);
|
||||
// }
|
||||
// catch
|
||||
// {
|
||||
// rt.SetValue(null, i);
|
||||
// }
|
||||
}
|
||||
|
||||
return rt;
|
||||
@ -94,8 +74,6 @@ namespace Esyur.Data
|
||||
{
|
||||
try
|
||||
{
|
||||
|
||||
|
||||
var underType = Nullable.GetUnderlyingType(destinationType);
|
||||
if (underType != null)
|
||||
{
|
||||
@ -105,7 +83,6 @@ namespace Esyur.Data
|
||||
destinationType = underType;
|
||||
}
|
||||
|
||||
|
||||
if (destinationType.IsInstanceOfType(value))
|
||||
{
|
||||
return value;
|
||||
@ -134,8 +111,6 @@ namespace Esyur.Data
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
public static byte[] ToBytes(sbyte value)
|
||||
{
|
||||
return new byte[1] { (byte)value };
|
||||
@ -602,8 +577,6 @@ namespace Esyur.Data
|
||||
return rt;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public static bool TryParse<T>(object Input, out T Results)
|
||||
{
|
||||
try
|
||||
@ -918,20 +891,6 @@ namespace Esyur.Data
|
||||
return rt;
|
||||
}
|
||||
|
||||
/*
|
||||
public static PhysicalAddress GetPhysicalAddress(this byte[] data, uint offset)
|
||||
{
|
||||
return new PhysicalAddress(Clip(data, offset, 6));
|
||||
}
|
||||
|
||||
public static PhysicalAddress[] GetPhysicalAddressArray(this byte[] data, uint offset, uint length)
|
||||
{
|
||||
var rt = new PhysicalAddress[length / 6];
|
||||
for (var i = 0; i < length; i += 6)
|
||||
rt[i] = GetPhysicalAddress(data, (uint)(offset + i));
|
||||
return rt;
|
||||
}
|
||||
*/
|
||||
public static IPAddress GetIPv4Address(this byte[] data, uint offset)
|
||||
{
|
||||
return new IPAddress((long)GetUInt32(data, offset));
|
||||
@ -958,83 +917,7 @@ namespace Esyur.Data
|
||||
return rt;
|
||||
}
|
||||
|
||||
/*
|
||||
public static T FromBytes<T>(byte[] data, uint offset, uint length = 0)
|
||||
{
|
||||
if (typeof(T) == typeof(bool))
|
||||
{
|
||||
return (T)(object)(data[offset] == 1);
|
||||
}
|
||||
else if (typeof(T) == typeof(byte))
|
||||
{
|
||||
return (T)(object)data[offset];
|
||||
}
|
||||
else if (typeof(T) == typeof(char))
|
||||
{
|
||||
return (T)(object)BitConverter.ToChar(ReverseArray(data, offset, 2), 0);
|
||||
}
|
||||
else if (typeof(T) == typeof(short))
|
||||
{
|
||||
return (T)(object)BitConverter.ToInt16(ReverseArray(data, offset, 2), 0);
|
||||
}
|
||||
else if (typeof(T) == typeof(ushort))
|
||||
{
|
||||
return (T)(object)BitConverter.ToUInt16(ReverseArray(data, offset, 2), 0);
|
||||
}
|
||||
else if (typeof(T) == typeof(int))
|
||||
{
|
||||
return (T)(object)BitConverter.ToInt32(ReverseArray(data, offset, 4), 0);
|
||||
}
|
||||
else if (typeof(T) == typeof(uint))
|
||||
{
|
||||
return (T)(object)BitConverter.ToUInt32(ReverseArray(data, offset, 4), 0);
|
||||
}
|
||||
else if (typeof(T) == typeof(long))
|
||||
{
|
||||
return (T)(object)BitConverter.ToInt64(ReverseArray(data, offset, 8), 0);
|
||||
}
|
||||
else if (typeof(T) == typeof(ulong))
|
||||
{
|
||||
return (T)(object)BitConverter.ToUInt64(ReverseArray(data, offset, 8), 0);
|
||||
}
|
||||
else if (typeof(T) == typeof(float))
|
||||
{
|
||||
return (T)(object)BitConverter.ToSingle(ReverseArray(data, offset, 4), 0);
|
||||
}
|
||||
else if (typeof(T) == typeof(double))
|
||||
{
|
||||
return (T)(object)BitConverter.ToDouble(ReverseArray(data, offset, 8), 0);
|
||||
}
|
||||
else if (typeof(T) == typeof(string))
|
||||
{
|
||||
return (T)(object)Encoding.UTF8.GetString(data, (int)offset, (int)length);
|
||||
}
|
||||
else if (typeof(T) == typeof(Guid))
|
||||
{
|
||||
return (T)(object)new Guid(DC.Clip(data, offset, 16));
|
||||
}
|
||||
else if (typeof(T) == typeof(IPAddress))
|
||||
{
|
||||
if (length == 0)
|
||||
return (T)(object)(new IPAddress((long)GetUInt32(data, offset)));
|
||||
else
|
||||
return (T)(object)(new IPAddress(Clip(data, offset, length)));
|
||||
}
|
||||
else if (typeof(T) == typeof(PhysicalAddress))
|
||||
{
|
||||
return (T)(object)new PhysicalAddress(Clip(data, offset, 6));
|
||||
}
|
||||
else if (typeof(T) == typeof(DateTime))
|
||||
{
|
||||
long ticks = BitConverter.ToInt64(ReverseArray(data, offset, 8), 0);
|
||||
return (T)(object)new DateTime(ticks, DateTimeKind.Utc);
|
||||
}
|
||||
else
|
||||
{
|
||||
return default(T);
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
|
||||
public static byte[] Clip(this byte[] data, uint offset, uint length)
|
||||
{
|
||||
|
274
Esyur/Data/ResourceList.cs
Normal file
274
Esyur/Data/ResourceList.cs
Normal file
@ -0,0 +1,274 @@
|
||||
/*
|
||||
|
||||
Copyright (c) 2020 Ahmed Kh. Zamil
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
|
||||
*/
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Collections;
|
||||
using Esyur.Core;
|
||||
using System.Reflection;
|
||||
|
||||
namespace Esyur.Data
|
||||
{
|
||||
public class ResourceList<T, ST> : IEnumerable<T>, ICollection, ICollection<T>
|
||||
{
|
||||
|
||||
private readonly object syncRoot = new object();
|
||||
private List<T> list = new List<T>();
|
||||
|
||||
public delegate void Modified(ST sender, int index, T oldValue, T newValue);
|
||||
public delegate void Added(ST sender, T value);
|
||||
public delegate void Removed(ST sender, int index, T value);
|
||||
public delegate void Cleared(ST sender);
|
||||
|
||||
|
||||
public event Modified OnModified;
|
||||
public event Removed OnRemoved;
|
||||
public event Cleared OnCleared;
|
||||
public event Added OnAdd;
|
||||
|
||||
ST state;
|
||||
|
||||
public void Sort()
|
||||
{
|
||||
list.Sort();
|
||||
}
|
||||
|
||||
public void Sort(IComparer<T> comparer)
|
||||
{
|
||||
list.Sort(comparer);
|
||||
}
|
||||
|
||||
public void Sort(Comparison<T> comparison)
|
||||
{
|
||||
list.Sort(comparison);
|
||||
}
|
||||
|
||||
public IEnumerable<T> Where(Func<T, bool> predicate)
|
||||
{
|
||||
return list.Where(predicate);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Convert AutoList to array
|
||||
/// </summary>
|
||||
/// <returns>Array</returns>
|
||||
public T[] ToArray()
|
||||
{
|
||||
// list.OrderBy()
|
||||
return list.ToArray();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Create a new instance of AutoList
|
||||
/// </summary>
|
||||
/// <param name="state">State object to be included when an event is raised.</param>
|
||||
public ResourceList(ST state)
|
||||
{
|
||||
this.state = state;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Create a new instance of AutoList
|
||||
/// </summary>
|
||||
/// <param name="values">Populate the list with items</param>
|
||||
/// <returns></returns>
|
||||
public ResourceList(ST state, T[] values)
|
||||
{
|
||||
this.state = state;
|
||||
AddRange(values);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Synchronization lock of the list
|
||||
/// </summary>
|
||||
public object SyncRoot
|
||||
{
|
||||
get
|
||||
{
|
||||
return syncRoot;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// First item in the list
|
||||
/// </summary>
|
||||
public T First()
|
||||
{
|
||||
return list.First();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get an item at a specified index
|
||||
/// </summary>
|
||||
public T this[int index]
|
||||
{
|
||||
get
|
||||
{
|
||||
return list[index];
|
||||
}
|
||||
set
|
||||
{
|
||||
var oldValue = list[index];
|
||||
|
||||
lock (syncRoot)
|
||||
list[index] = value;
|
||||
|
||||
OnModified?.Invoke(state, index, oldValue, value);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Add item to the list
|
||||
/// </summary>
|
||||
public void Add(T value)
|
||||
{
|
||||
lock (syncRoot)
|
||||
list.Add(value);
|
||||
|
||||
OnAdd?.Invoke(state, value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Add an array of items to the list
|
||||
/// </summary>
|
||||
public void AddRange(T[] values)
|
||||
{
|
||||
foreach (var v in values)
|
||||
Add(v);
|
||||
}
|
||||
|
||||
private void ItemDestroyed(object sender)
|
||||
{
|
||||
Remove((T)sender);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Clear the list
|
||||
/// </summary>
|
||||
public void Clear()
|
||||
{
|
||||
|
||||
lock (syncRoot)
|
||||
list.Clear();
|
||||
|
||||
OnCleared?.Invoke(state);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Remove an item from the list
|
||||
/// <param name="value">Item to remove</param>
|
||||
/// </summary>
|
||||
public void Remove(T value)
|
||||
{
|
||||
var index = 0;
|
||||
|
||||
lock (syncRoot)
|
||||
{
|
||||
index = list.IndexOf(value);
|
||||
|
||||
if (index == -1)
|
||||
return;
|
||||
|
||||
list.RemoveAt(index);
|
||||
|
||||
|
||||
}
|
||||
|
||||
OnRemoved?.Invoke(state, index, value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Number of items in the list
|
||||
/// </summary>
|
||||
public int Count
|
||||
{
|
||||
get { return list.Count; }
|
||||
}
|
||||
|
||||
public bool IsSynchronized => (list as ICollection).IsSynchronized;
|
||||
|
||||
public bool IsReadOnly => throw new NotImplementedException();
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Check if an item exists in the list
|
||||
/// </summary>
|
||||
/// <param name="value">Item to check if exists</param>
|
||||
public bool Contains(T value)
|
||||
{
|
||||
return list.Contains(value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Check if any item of the given array is in the list
|
||||
/// </summary>
|
||||
/// <param name="values">Array of items</param>
|
||||
public bool ContainsAny(T[] values)
|
||||
{
|
||||
foreach (var v in values)
|
||||
if (list.Contains(v))
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Check if any item of the given list is in the list
|
||||
/// </summary>
|
||||
/// <param name="values">List of items</param>
|
||||
public bool ContainsAny(AutoList<T, ST> values)
|
||||
{
|
||||
foreach (var v in values)
|
||||
if (list.Contains((T)v))
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
public IEnumerator<T> GetEnumerator()
|
||||
{
|
||||
return ((IEnumerable<T>)list).GetEnumerator();
|
||||
}
|
||||
|
||||
IEnumerator IEnumerable.GetEnumerator()
|
||||
{
|
||||
return ((IEnumerable<T>)list).GetEnumerator();
|
||||
}
|
||||
|
||||
public void CopyTo(Array array, int index)
|
||||
{
|
||||
(list as ICollection).CopyTo(array, index);
|
||||
}
|
||||
|
||||
public void CopyTo(T[] array, int arrayIndex)
|
||||
{
|
||||
list.CopyTo(array, arrayIndex);
|
||||
}
|
||||
|
||||
bool ICollection<T>.Remove(T item)
|
||||
{
|
||||
return list.Remove(item);
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user