diff --git a/src/Volo.Abp/Volo/Abp/Collections/ITypeList.cs b/src/Volo.Abp/Volo/Abp/Collections/ITypeList.cs new file mode 100644 index 0000000000..76931accee --- /dev/null +++ b/src/Volo.Abp/Volo/Abp/Collections/ITypeList.cs @@ -0,0 +1,39 @@ +using System; +using System.Collections.Generic; + +namespace Volo.Abp.Collections +{ + /// + /// A shortcut for to use object as base type. + /// + public interface ITypeList : ITypeList + { + + } + + /// + /// Extends to add restriction a specific base type. + /// + /// Base Type of s in this list + public interface ITypeList : IList + { + /// + /// Adds a type to list. + /// + /// Type + void Add() where T : TBaseType; + + /// + /// Checks if a type exists in the list. + /// + /// Type + /// + bool Contains() where T : TBaseType; + + /// + /// Removes a type from list + /// + /// + void Remove() where T : TBaseType; + } +} \ No newline at end of file diff --git a/src/Volo.Abp/Volo/Abp/Collections/TypeList.cs b/src/Volo.Abp/Volo/Abp/Collections/TypeList.cs new file mode 100644 index 0000000000..e96d40f213 --- /dev/null +++ b/src/Volo.Abp/Volo/Abp/Collections/TypeList.cs @@ -0,0 +1,144 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using System.Reflection; + +namespace Volo.Abp.Collections +{ + /// + /// A shortcut for to use object as base type. + /// + public class TypeList : TypeList, ITypeList + { + } + + /// + /// Extends to add restriction a specific base type. + /// + /// Base Type of s in this list + public class TypeList : ITypeList + { + /// + /// Gets the count. + /// + /// The count. + public int Count => _typeList.Count; + + /// + /// Gets a value indicating whether this instance is read only. + /// + /// true if this instance is read only; otherwise, false. + public bool IsReadOnly => false; + + /// + /// Gets or sets the at the specified index. + /// + /// Index. + public Type this[int index] + { + get { return _typeList[index]; } + set + { + CheckType(value); + _typeList[index] = value; + } + } + + private readonly List _typeList; + + /// + /// Creates a new object. + /// + public TypeList() + { + _typeList = new List(); + } + + /// + public void Add() where T : TBaseType + { + _typeList.Add(typeof(T)); + } + + /// + public void Add(Type item) + { + CheckType(item); + _typeList.Add(item); + } + + /// + public void Insert(int index, Type item) + { + CheckType(item); + _typeList.Insert(index, item); + } + + /// + public int IndexOf(Type item) + { + return _typeList.IndexOf(item); + } + + /// + public bool Contains() where T : TBaseType + { + return Contains(typeof(T)); + } + + /// + public bool Contains(Type item) + { + return _typeList.Contains(item); + } + + /// + public void Remove() where T : TBaseType + { + _typeList.Remove(typeof(T)); + } + + /// + public bool Remove(Type item) + { + return _typeList.Remove(item); + } + + /// + public void RemoveAt(int index) + { + _typeList.RemoveAt(index); + } + + /// + public void Clear() + { + _typeList.Clear(); + } + + /// + public void CopyTo(Type[] array, int arrayIndex) + { + _typeList.CopyTo(array, arrayIndex); + } + + /// + public IEnumerator GetEnumerator() + { + return _typeList.GetEnumerator(); + } + + IEnumerator IEnumerable.GetEnumerator() + { + return _typeList.GetEnumerator(); + } + + private static void CheckType(Type item) + { + if (!typeof(TBaseType).GetTypeInfo().IsAssignableFrom(item)) + { + throw new ArgumentException($"Given type ({item.AssemblyQualifiedName}) should be instance of {typeof(TBaseType).AssemblyQualifiedName} ", nameof(item)); + } + } + } +} \ No newline at end of file diff --git a/src/Volo.ExtensionMethods/Volo/ExtensionMethods/Collections/Generic/ListExtensions.cs b/src/Volo.ExtensionMethods/Volo/ExtensionMethods/Collections/Generic/ListExtensions.cs index cf69ddb329..ed5729d8a1 100644 --- a/src/Volo.ExtensionMethods/Volo/ExtensionMethods/Collections/Generic/ListExtensions.cs +++ b/src/Volo.ExtensionMethods/Volo/ExtensionMethods/Collections/Generic/ListExtensions.cs @@ -27,7 +27,7 @@ namespace Volo.ExtensionMethods.Collections.Generic } /// - /// Sort a list by a topological sorting, which consider their dependencies + /// Sort a list by a topological sorting, which consider their dependencies. /// /// The type of the members of values. /// A list of objects to sort