1
This commit is contained in:
@@ -50,6 +50,17 @@ namespace BITKit
|
||||
key = Generic<T>.GetVariable(key);
|
||||
events.Get(key).Remove(action);
|
||||
}
|
||||
|
||||
public static void Set<T>(string key, Func<T> factory)
|
||||
{
|
||||
key = Generic<T>.GetVariable(key);
|
||||
dictionary.AddOrUpdate(key,factory, AddOrUpdate);
|
||||
return;
|
||||
Func<T> AddOrUpdate(string _key,object current)
|
||||
{
|
||||
return factory;
|
||||
}
|
||||
}
|
||||
public static void Set<T>(string key, T value)
|
||||
{
|
||||
key = Generic<T>.GetVariable(key);
|
||||
@@ -91,9 +102,12 @@ namespace BITKit
|
||||
key = Generic<T>.GetVariable(key);
|
||||
if (dictionary.TryGetValue(key, out var value))
|
||||
{
|
||||
if (value is T t)
|
||||
switch (value)
|
||||
{
|
||||
return t;
|
||||
case T t:
|
||||
return t;
|
||||
case Func<T> func:
|
||||
return func.Invoke();
|
||||
}
|
||||
}
|
||||
return default;
|
||||
|
@@ -25,10 +25,12 @@ namespace BITKit
|
||||
else if (bool.TryParse(value, out var boolResult))
|
||||
{
|
||||
Data.Set(key, boolResult);
|
||||
Data.Set(key, boolResult ? 1 : 0);
|
||||
}
|
||||
else if (int.TryParse(value, out var intResult))
|
||||
{
|
||||
Data.Set(key, intResult);
|
||||
Data.Set(key, intResult is 1);
|
||||
}
|
||||
else if (float.TryParse(value, out var floatResult))
|
||||
{
|
||||
|
@@ -35,7 +35,7 @@ namespace BITKit
|
||||
public static void EnsureDirectoryCreated(string path)
|
||||
{
|
||||
path = Path.GetDirectoryName(path);
|
||||
if (Directory.Exists(path) is true) return;
|
||||
if (Directory.Exists(path)) return;
|
||||
if (path != null) Directory.CreateDirectory(path);
|
||||
}
|
||||
|
||||
@@ -44,21 +44,17 @@ namespace BITKit
|
||||
var path = GetPath(paths);
|
||||
var dictionaryPath = Path.GetDirectoryName(path);
|
||||
Directory.CreateDirectory(dictionaryPath);
|
||||
if (File.Exists(path) is false)
|
||||
{
|
||||
using (var fs = File.Create(path))
|
||||
{
|
||||
fs.Close();
|
||||
fs.Dispose();
|
||||
}
|
||||
}
|
||||
if (File.Exists(path)) return path;
|
||||
using var fs = File.Create(path);
|
||||
fs.Close();
|
||||
fs.Dispose();
|
||||
|
||||
return path;
|
||||
}
|
||||
|
||||
public static string GetTempFilePath(string fileName = null)
|
||||
{
|
||||
var path = GetFilePath("Temps", fileName is null ? Guid.NewGuid().ToString() : fileName);
|
||||
var path = GetFilePath("Temps", fileName ?? Guid.NewGuid().ToString());
|
||||
return path;
|
||||
}
|
||||
|
||||
|
@@ -12,13 +12,14 @@ namespace BITKit
|
||||
{
|
||||
public class ReflectionHelper
|
||||
{
|
||||
public static BindingFlags Flags => BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic;
|
||||
public static InitializationState State = InitializationState.None;
|
||||
private static Type[] types = Type.EmptyTypes;
|
||||
private static IEnumerable<MethodInfo> methods = new List<MethodInfo>();
|
||||
private static IEnumerable<FieldInfo> fields = new List<FieldInfo>();
|
||||
private readonly static Dictionary<Type, MethodInfo[]> methodsCache = new();
|
||||
private readonly static Dictionary<Type, FieldInfo[]> fieldsCache = new();
|
||||
private readonly static Dictionary<Attribute, Attribute[]> attributes = new();
|
||||
private static readonly Dictionary<Type, MethodInfo[]> methodsCache = new();
|
||||
private static readonly Dictionary<Type, FieldInfo[]> fieldsCache = new();
|
||||
private static readonly Dictionary<Attribute, Attribute[]> attributes = new();
|
||||
public static async Task<IEnumerable<MethodInfo>> GetMethods<Att>() where Att : Attribute
|
||||
{
|
||||
await EnsureConfig();
|
||||
@@ -145,7 +146,7 @@ namespace BITKit
|
||||
|
||||
var loadedAssemblies = BITApp.Assemblies.IsValid()
|
||||
? BITApp.Assemblies
|
||||
: AppDomain.CurrentDomain.GetAssemblies();
|
||||
: GetAllAssemblies();
|
||||
BIT4Log.Log<ReflectionHelper>($"已加载程序集:{loadedAssemblies.Length}个");
|
||||
var result = new List<Type>();
|
||||
for (var i = 0; i < loadedAssemblies.Length; i++)
|
||||
@@ -226,6 +227,23 @@ namespace BITKit
|
||||
State = InitializationState.Initialized;
|
||||
BIT4Log.Log<ReflectionHelper>("已完成初始化");
|
||||
}
|
||||
|
||||
private static Assembly[] GetAllAssemblies()
|
||||
{
|
||||
return AppDomain.CurrentDomain.GetAssemblies();
|
||||
//return AppDomain.CurrentDomain.GetReferanceAssemblies().ToArray();
|
||||
// var assemblies = new List<Assembly>();
|
||||
// foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
|
||||
// {
|
||||
// assemblies.Add(assembly);
|
||||
// foreach (var assemblyName in assembly.GetReferencedAssemblies())
|
||||
// {
|
||||
// assemblies.Add(Assembly.Load(assemblyName));
|
||||
// }
|
||||
// }
|
||||
// return assemblies.Distinct().ToArray();
|
||||
}
|
||||
|
||||
[ExcuteOnStop]
|
||||
public static void Reload()
|
||||
{
|
||||
@@ -237,4 +255,36 @@ namespace BITKit
|
||||
methodsCache.Clear();
|
||||
}
|
||||
}
|
||||
public static class Extents
|
||||
{
|
||||
public static List<Assembly> GetReferanceAssemblies(this AppDomain domain)
|
||||
{
|
||||
var list = new List<Assembly>();
|
||||
domain.GetAssemblies().ForEach(i =>
|
||||
{
|
||||
GetReferanceAssemblies(i, list);
|
||||
});
|
||||
return list;
|
||||
}
|
||||
static void GetReferanceAssemblies(Assembly assembly, List<Assembly> list)
|
||||
{
|
||||
assembly.GetReferencedAssemblies().ForEach(i =>
|
||||
{
|
||||
try
|
||||
{
|
||||
var ass = Assembly.Load(i);
|
||||
if (!list.Contains(ass))
|
||||
{
|
||||
list.Add(ass);
|
||||
GetReferanceAssemblies(ass, list);
|
||||
}
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
// ignored
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user