Commit 066fa075 authored by 阿星Plus's avatar 阿星Plus

😀 提取扩展方法到 Plus.Extensions .移除多余 using

parent e1abb5b0
This source diff could not be displayed because it is too large. You can view the blob instead.
using Plus.Collections; using Plus.Service;
using Plus.Service;
using System; using System;
namespace Plus.Aspects namespace Plus.Aspects
......
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Runtime.ExceptionServices;
using System.Threading.Tasks;
namespace Plus.Collections
{
/// <summary>
/// 扩展方法,后面要拿出来单独放一个项目中
/// </summary>
public static class Extensions
{
public static bool IsAsync(this MethodInfo method)
{
return method.ReturnType == typeof(Task) || (method.ReturnType.GetTypeInfo().IsGenericType && method.ReturnType.GetGenericTypeDefinition() == typeof(Task<>));
}
public static List<T> SortByDependencies<T>(this IEnumerable<T> source, Func<T, IEnumerable<T>> getDependencies)
{
var sorted = new List<T>();
var visited = new Dictionary<T, bool>();
foreach (var item in source)
{
SortByDependenciesVisit(item, getDependencies, sorted, visited);
}
return sorted;
}
private static void SortByDependenciesVisit<T>(T item, Func<T, IEnumerable<T>> getDependencies, List<T> sorted, Dictionary<T, bool> visited)
{
var alreadyVisited = visited.TryGetValue(item, out bool inProcess);
if (alreadyVisited)
{
if (inProcess)
{
throw new ArgumentException("Cyclic dependency found! Item: " + item);
}
}
else
{
visited[item] = true;
var dependencies = getDependencies(item);
if (dependencies != null)
{
foreach (var dependency in dependencies)
{
SortByDependenciesVisit(dependency, getDependencies, sorted, visited);
}
}
visited[item] = false;
sorted.Add(item);
}
}
public static bool IsNullOrEmpty<T>(this ICollection<T> source)
{
return source == null || source.Count <= 0;
}
public static bool AddIfNotContains<T>(this ICollection<T> source, T item)
{
if (source == null)
{
throw new ArgumentNullException("source");
}
if (source.Contains(item))
{
return false;
}
source.Add(item);
return true;
}
/// <summary>
/// This method is used to try to get a value in a dictionary if it does exists.
/// </summary>
/// <typeparam name="T">Type of the value</typeparam>
/// <param name="dictionary">The collection object</param>
/// <param name="key">Key</param>
/// <param name="value">Value of the key (or default value if key not exists)</param>
/// <returns>True if key does exists in the dictionary</returns>
internal static bool TryGetValue<T>(this IDictionary<string, object> dictionary, string key, out T value)
{
object valueObj;
if (dictionary.TryGetValue(key, out valueObj) && valueObj is T)
{
value = (T)valueObj;
return true;
}
value = default(T);
return false;
}
/// <summary>
/// Gets a value from the dictionary with given key. Returns default value if can not find.
/// </summary>
/// <param name="dictionary">Dictionary to check and get</param>
/// <param name="key">Key to find the value</param>
/// <typeparam name="TKey">Type of the key</typeparam>
/// <typeparam name="TValue">Type of the value</typeparam>
/// <returns>Value if found, default if can not found.</returns>
public static TValue GetOrDefault<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key)
{
TValue obj;
return dictionary.TryGetValue(key, out obj) ? obj : default(TValue);
}
/// <summary>
/// Gets a value from the dictionary with given key. Returns default value if can not find.
/// </summary>
/// <param name="dictionary">Dictionary to check and get</param>
/// <param name="key">Key to find the value</param>
/// <param name="factory">A factory method used to create the value if not found in the dictionary</param>
/// <typeparam name="TKey">Type of the key</typeparam>
/// <typeparam name="TValue">Type of the value</typeparam>
/// <returns>Value if found, default if can not found.</returns>
public static TValue GetOrAdd<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key, Func<TKey, TValue> factory)
{
TValue obj;
if (dictionary.TryGetValue(key, out obj))
{
return obj;
}
return dictionary[key] = factory(key);
}
/// <summary>
/// Gets a value from the dictionary with given key. Returns default value if can not find.
/// </summary>
/// <param name="dictionary">Dictionary to check and get</param>
/// <param name="key">Key to find the value</param>
/// <param name="factory">A factory method used to create the value if not found in the dictionary</param>
/// <typeparam name="TKey">Type of the key</typeparam>
/// <typeparam name="TValue">Type of the value</typeparam>
/// <returns>Value if found, default if can not found.</returns>
public static TValue GetOrAdd<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key, Func<TValue> factory)
{
return dictionary.GetOrAdd(key, k => factory());
}
public static Assembly GetAssembly(this Type type)
{
return type.GetTypeInfo().Assembly;
}
public static MethodInfo GetMethod(this Type type, string methodName, int pParametersCount = 0, int pGenericArgumentsCount = 0)
{
return type
.GetMethods()
.Where(m => m.Name == methodName).ToList()
.Select(m => new
{
Method = m,
Params = m.GetParameters(),
Args = m.GetGenericArguments()
})
.Where(x => x.Params.Length == pParametersCount
&& x.Args.Length == pGenericArgumentsCount
).Select(x => x.Method)
.First();
}
/// <summary>
/// Executes given <paramref name="action"/> by locking given <paramref name="source"/> object.
/// </summary>
/// <typeparam name="T">Type of the object (to be locked)</typeparam>
/// <param name="source">Source object (to be locked)</param>
/// <param name="action">Action (to be executed)</param>
public static void Locking<T>(this T source, Action<T> action) where T : class
{
lock (source)
{
action(source);
}
}
public static void ReThrow(this Exception exception)
{
ExceptionDispatchInfo.Capture(exception).Throw();
}
}
}
\ No newline at end of file
using Plus.Collections; using System;
using System;
using System.Collections.Generic; using System.Collections.Generic;
namespace Plus.Configuration namespace Plus.Configuration
......
using Castle.Core.Logging; using Castle.Core.Logging;
using Plus.Collections;
using Plus.Event.Bus.Factories; using Plus.Event.Bus.Factories;
using Plus.Event.Bus.Factories.Internals; using Plus.Event.Bus.Factories.Internals;
using Plus.Event.Bus.Handlers; using Plus.Event.Bus.Handlers;
......
using Castle.Core.Logging; using Castle.Core.Logging;
using Plus.Collections;
using Plus.Configuration.Startup; using Plus.Configuration.Startup;
using Plus.Dependency; using Plus.Dependency;
using System; using System;
......
using Plus.Collections; using System;
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
......
...@@ -11,4 +11,8 @@ ...@@ -11,4 +11,8 @@
<PackageReference Include="System.ComponentModel.Annotations" Version="4.5.0" /> <PackageReference Include="System.ComponentModel.Annotations" Version="4.5.0" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Plus.Extensions\Plus.Extensions.csproj" />
</ItemGroup>
</Project> </Project>
using Castle.MicroKernel.Registration; using Castle.MicroKernel.Registration;
using Plus.Collections;
using Plus.Configuration.Startup; using Plus.Configuration.Startup;
using Plus.Dependency; using Plus.Dependency;
using Plus.Event.Bus; using Plus.Event.Bus;
using Plus.Modules; using Plus.Modules;
using System; using System;
using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq.Expressions; using System.Linq.Expressions;
......
using Plus.Dependency; using Plus.Configuration.Startup;
using Plus.Dependency;
using Plus.Reflection;
using System; using System;
using System.Collections.Generic;
using System.Text;
using System.Collections; using System.Collections;
using System.Collections.Generic;
using System.ComponentModel; using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq; using System.Linq;
using System.Reflection; using System.Reflection;
using Plus;
using Plus.Configuration.Startup;
using Plus.Runtime.Validation;
using Plus.Runtime.Validation.Interception;
using System.ComponentModel.DataAnnotations;
using Plus.Collections;
using Plus.Reflection;
namespace Plus.Runtime.Validation.Interception namespace Plus.Runtime.Validation.Interception
{ {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment