Просмотр исходного кода

升级到 .NET Standard 2.0 / .NET Framework 4.8

龙腾猫跃 5 месяцев назад
Родитель
Сommit
58eea0e113
3 измененных файлов с 36 добавлено и 37 удалено
  1. 35 34
      Shared/General/ConcurrentList.cs
  2. 0 1
      Shared/MeloongCore.csproj
  3. 1 2
      Wpf/MeloongCoreWpf.csproj

+ 35 - 34
Shared/General/ConcurrentList.cs

@@ -7,60 +7,61 @@
 public class ConcurrentList<T> : IList<T>, IList, IEnumerable, IEnumerable<T> {
 
     private readonly List<T> _items = [];
-    private readonly object _lock = new();
+    public readonly object syncLock = new();
 
     // 构造函数
     public ConcurrentList() { }
-    public ConcurrentList(IEnumerable<T> data) { _items = new List<T>(data); }
+    public ConcurrentList(IEnumerable<T> data) => _items = new List<T>(data);
     public static implicit operator ConcurrentList<T>(List<T> data) => new(data);
-    public static implicit operator List<T>(ConcurrentList<T> data) { lock (data._lock) { return new List<T>(data._items); } }
+    public static implicit operator List<T>(ConcurrentList<T> data) { lock (data.syncLock) { return new List<T>(data._items); } }
 
-    public int Count { get { lock (_lock) { return _items.Count; } } }
+    // 枚举器
+    public IEnumerator<T> GetEnumerator() {
+        lock (syncLock) {
+            return _items.ToList().GetEnumerator();
+        }
+    }
+    IEnumerator IEnumerable.GetEnumerator() {
+        lock (syncLock) {
+            return _items.ToList().GetEnumerator();
+        }
+    }
+
+    // 成员
+    public int Count { get { lock (syncLock) { return _items.Count; } } }
     public bool IsReadOnly => false;
     bool IList.IsFixedSize => false;
     bool ICollection.IsSynchronized => true;
-    object ICollection.SyncRoot => _lock;
+    object ICollection.SyncRoot => syncLock;
 
     public T this[int index] {
-        get { lock (_lock) { return _items[index]; } }
-        set { lock (_lock) { _items[index] = value; } }
+        get { lock (syncLock) { return _items[index]; } }
+        set { lock (syncLock) { _items[index] = value; } }
     }
     object? IList.this[int index] {
         get => this[index];
-        set { lock (_lock) { _items[index] = (T)value!; } }
+        set { lock (syncLock) { _items[index] = (T) value!; } }
     }
 
-    public void Add(T item) { lock (_lock) { _items.Add(item); } }
-    int IList.Add(object? value) { lock (_lock) { _items.Add((T)value!); return _items.Count - 1; } }
+    public void Add(T item) { lock (syncLock) { _items.Add(item); } }
+    int IList.Add(object? value) { lock (syncLock) { _items.Add((T) value!); return _items.Count - 1; } }
 
-    public void Insert(int index, T item) { lock (_lock) { _items.Insert(index, item); } }
-    void IList.Insert(int index, object? value) { lock (_lock) { _items.Insert(index, (T)value!); } }
+    public void Insert(int index, T item) { lock (syncLock) { _items.Insert(index, item); } }
+    void IList.Insert(int index, object? value) { lock (syncLock) { _items.Insert(index, (T) value!); } }
 
-    public bool Remove(T item) { lock (_lock) { return _items.Remove(item); } }
-    void IList.Remove(object? value) { lock (_lock) { if (value is T t) _items.Remove(t); } }
+    public bool Remove(T item) { lock (syncLock) { return _items.Remove(item); } }
+    void IList.Remove(object? value) { lock (syncLock) { if (value is T t) _items.Remove(t); } }
 
-    public void RemoveAt(int index) { lock (_lock) { _items.RemoveAt(index); } }
-    public void Clear() { lock (_lock) { _items.Clear(); } }
+    public void RemoveAt(int index) { lock (syncLock) { _items.RemoveAt(index); } }
+    public void Clear() { lock (syncLock) { _items.Clear(); } }
 
-    public bool Contains(T item) { lock (_lock) { return _items.Contains(item); } }
-    bool IList.Contains(object? value) { lock (_lock) { return value is T t && _items.Contains(t); } }
+    public bool Contains(T item) { lock (syncLock) { return _items.Contains(item); } }
+    bool IList.Contains(object? value) { lock (syncLock) { return value is T t && _items.Contains(t); } }
 
-    public int IndexOf(T item) { lock (_lock) { return _items.IndexOf(item); } }
-    int IList.IndexOf(object? value) { lock (_lock) { return value is T t ? _items.IndexOf(t) : -1; } }
+    public int IndexOf(T item) { lock (syncLock) { return _items.IndexOf(item); } }
+    int IList.IndexOf(object? value) { lock (syncLock) { return value is T t ? _items.IndexOf(t) : -1; } }
 
-    public void CopyTo(T[] array, int arrayIndex) { lock (_lock) { _items.CopyTo(array, arrayIndex); } }
-    void ICollection.CopyTo(Array array, int index) { lock (_lock) { ((ICollection)_items).CopyTo(array, index); } }
-
-    // 对枚举器进行覆写,返回一个临时副本
-    public IEnumerator<T> GetEnumerator() {
-        lock (_lock) {
-            return _items.ToList().GetEnumerator();
-        }
-    }
-    IEnumerator IEnumerable.GetEnumerator() {
-        lock (_lock) {
-            return _items.ToList().GetEnumerator();
-        }
-    }
+    public void CopyTo(T[] array, int arrayIndex) { lock (syncLock) { _items.CopyTo(array, arrayIndex); } }
+    void ICollection.CopyTo(Array array, int index) { lock (syncLock) { ((ICollection) _items).CopyTo(array, index); } }
 
 }

+ 0 - 1
Shared/MeloongCore.csproj

@@ -20,7 +20,6 @@
 	    <PrivateAssets>all</PrivateAssets>
 	    <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
 	  </PackageReference>
-	  <PackageReference Include="System.ValueTuple" Version="4.6.1" />
 	</ItemGroup>
 
 	<ItemGroup>

+ 1 - 2
Wpf/MeloongCoreWpf.csproj

@@ -1,7 +1,7 @@
 <Project Sdk="Microsoft.NET.Sdk">
 
 	<PropertyGroup>
-		<TargetFramework>net462</TargetFramework>
+		<TargetFramework>net48</TargetFramework>
 		<OutputType>Library</OutputType>
 		<AllowUnsafeBlocks>True</AllowUnsafeBlocks>
 		<OutDir>bin\</OutDir>
@@ -22,7 +22,6 @@
 	    <PrivateAssets>all</PrivateAssets>
 	    <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
 	  </PackageReference>
-	  <PackageReference Include="System.ValueTuple" Version="4.6.1" />
 	</ItemGroup>
 
 	<ItemGroup>