private static Func <object> GetObjectBuilderOf(ILake lake, Type t) { if (lake is null) { throw new ArgumentNullException(nameof(lake)); } if (t is null) { throw new ArgumentNullException(nameof(t)); } return(() => { if (t.IsInterface || t.IsAbstract) { throw new InvalidOperationException("Could not create instance of interface or abstract class"); } IProxyBuilder proxyBuilder; if (lake.TryGet(out IProxyBuilder value)) { proxyBuilder = value; } else { proxyBuilder = defaultProxyBuilder; } var proxy = proxyBuilder.CreateProxyOf(t); proxy.CreateInstance(); proxy.Lakes[0] = lake; return proxy.Instance; }); }
internal static Task <object?> CreateTask(IExtensionInfo info, Dictionary <string, object> args, Action <Thread> threadReceiver, ILake source) { return(new Task <object?>(() => { try { Thread.CurrentThread.Name = $"Extension Task {info.Id}"; using var procedure = info.OpenProcedure(); threadReceiver(Thread.CurrentThread); procedure.Source = source; procedure.Args = args; return procedure.Run(); } catch (Exception e) { SLogger.Warn("ExtensionTask", "Uncaught error", e); var title = string.Format(new T().RxGetClassText("error_title_fmt"), info.Name()); LakeProvider.Lake.Get <IAppManager>().ShowException(title, e.GetType().Name, e); return default; } })); }
public void RegisterTest() { ILake factory = APIFactory.Instance; factory.Register <string>(() => "test"); Assert.IsTrue(factory.Get <string>() == "test"); }
public static IEnumerable <object> GetComponents(this ILake lake, string id) { const string METHOD_NAME = "GetComponents"; if (lake is null) { throw new ArgumentNullException(nameof(lake)); } if (string.IsNullOrEmpty(id)) { throw new ArgumentException("message", nameof(id)); } var method = lake.GetType().GetMethod(METHOD_NAME, BindingFlags.Public | BindingFlags.Instance); if (method == null || method.ReturnType != typeof(IEnumerable <object>) || method.GetParameters().Length != 1 || method.GetParameters()[0].ParameterType != typeof(string)) { return(new object[] { lake.GetComponent(id) }); //throw new NotSupportedException($"This lake ({lake?.GetType()?.FullName}) does not support: IEnumerable<object> GetComponents(string)"); } return((IEnumerable <object>)method.Invoke(lake, new object[] { id })); }
static LakeProvider() { if (Lake == null) { Lake = null; } }
/// <summary> /// 注册 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="lake"></param> /// <param name="value"></param> /// <returns></returns> public static ILake RegisterSingleton <T>(this ILake lake, T value) { if (lake is null) { throw new ArgumentNullException(nameof(lake)); } return(lake.Register <T>(() => value)); }
/// <summary> /// 泛型地获取一个值 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="lake"></param> /// <returns></returns> public static T Get <T>(this ILake lake) { if (lake is null) { throw new ArgumentNullException(nameof(lake)); } return((T)lake.Get(typeof(T))); }
/// <summary> /// 注册 /// </summary> /// <typeparam name="T"></typeparam> /// <typeparam name="TImpl"></typeparam> /// <returns></returns> public static ILake RegisterSingleton <T, TImpl>(this ILake lake) where TImpl : T { if (lake is null) { throw new ArgumentNullException(nameof(lake)); } return(lake.RegisterSingleton <T>(typeof(TImpl))); }
public static ILake UniteWith(this ILake first, params ILake[] others) { List <ILake> tmp = new List <ILake> { [0] = first }; tmp.AddRange(others); return(new MergedLake(tmp.ToArray())); }
public F**k(int test, string test2, ILake lake) { if (lake is null) { throw new ArgumentNullException(nameof(lake)); } this.Test = test; this.Test2 = test2; Lake = lake; }
public void RegisterSingletonTest() { ILake factory = APIFactory.Instance; Random ran = new Random(); factory.RegisterSingleton <int>(() => ran.Next()); Assert.IsTrue(factory.Get <int>() == factory.Get <int>()); factory.RegisterSingleton(typeof(int), () => ran.Next()); Assert.IsTrue(factory.Get <int>() == factory.Get <int>()); }
public void GetNotExistTest() { ILake factory = APIFactory.Instance; Exception ex = null; try { factory.Get <string>(); } catch (Exception e) { ex = e; } Assert.IsTrue(ex != null); }
/// <summary> /// 尝试获取 /// </summary> /// <param name="lake"></param> /// <param name="t"></param> /// <param name="value"></param> /// <returns></returns> public static bool TryGet(this ILake lake, Type t, out object value) { try { value = lake.Get(t); return(true); } catch { value = default; return(false); } }
/// <summary> /// 尝试获取 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="lake"></param> /// <param name="value"></param> /// <returns></returns> public static bool TryGet <T>(this ILake lake, out T value) { try { value = lake.Get <T>(); return(true); } catch { value = default; return(false); } }
public void InjectTest() { ILake factory = APIFactory.Instance; Random ran = new Random(); var number = ran.Next(); factory.RegisterSingleton <RequireValueClass>(typeof(RequireValueClass)); factory.RegisterSingleton <int>(() => number); Debug.WriteLine(number); var x = factory.Get <RequireValueClass>(); Debug.WriteLine(x.X); Assert.IsTrue(x.X == number); }
/// <summary> /// 注册 /// </summary> /// <typeparam name="T"></typeparam> /// <typeparam name="TImpl"></typeparam> /// <returns></returns> public static ILake Register <T, TImpl>(this ILake lake) where TImpl : T { if (lake is null) { throw new ArgumentNullException(nameof(lake)); } return(lake.Register(typeof(T), () => { var proxy = proxyBuilder.CreateProxyOf(typeof(TImpl)); proxy.CreateInstance(); return proxy.Instance; })); }
/// <summary> /// 注册 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="lake"></param> /// <param name="factory"></param> /// <returns></returns> public static ILake Register <T>(this ILake lake, Func <object> factory) { if (lake is null) { throw new ArgumentNullException(nameof(lake)); } if (factory is null) { throw new ArgumentNullException(nameof(factory)); } return(lake.Register(typeof(T), factory)); }
public void Run() { int[] nums = Console.ReadLine().Split(", ").Select(int.Parse).ToArray(); this.lake = new Lake(nums); var sb = new StringBuilder(); foreach (var num in this.lake) { sb.Append($"{num}, "); } Console.WriteLine(sb.ToString().TrimEnd(' ', ',')); }
public static bool TryGet <T>(this ILake lake, out T value) { if (lake is null) { throw new ArgumentNullException(nameof(lake)); } try { value = lake.Get <T>(); return(true); } catch (TypeNotFoundException) { SLogger.Warn(nameof(LakeExtension), $"Can not get component by type: {typeof(T).FullName}"); value = default !;
private static ComponentFactory GetObjectBuilderOf(ILake lake, Type t) { if (lake is null) { throw new ArgumentNullException(nameof(lake)); } if (t is null) { throw new ArgumentNullException(nameof(t)); } if (t.IsInterface || t.IsAbstract) { throw new InvalidOperationException("Could not create instance of interface or abstract class"); } return(() => lake.CreateInstance(t)); }
public static object?[] BuildArgs( ILake lake, Dictionary <string, object> extraArgs, ParameterInfo[] parameterInfos) { if (lake is null) { throw new System.ArgumentNullException(nameof(lake)); } if (extraArgs is null) { throw new System.ArgumentNullException(nameof(extraArgs)); } if (parameterInfos is null) { throw new System.ArgumentNullException(nameof(parameterInfos)); } List <object?> args = new List <object?>(); foreach (var p in parameterInfos) { if (extraArgs.TryGetValue(p.Name, out object value)) { args.Add(value); } else if (lake.TryGet(p.Name, out object?byNameValue)) { args.Add(byNameValue); } else if (lake.TryGet(p.ParameterType, out object?byTypeValue)) { args.Add(byTypeValue); } else { args.Add(p.DefaultValue); } } return(args.ToArray()); }
public static bool TryGet <T>(this ILake lake, out T value) { if (lake is null) { #pragma warning disable CS8601 // 可能的 null 引用赋值。 value = default; #pragma warning restore CS8601 // 可能的 null 引用赋值。 return(false); } try { value = lake.Get <T>(); return(true); } catch (TypeNotFoundException) { SLogger.Warn(nameof(LakeExtension), $"Can not get component by type: {typeof(T).FullName}"); value = default !;
public static T Get <T>(this ILake lake) { if (lake is null) { throw new ArgumentNullException(nameof(lake)); } try { #pragma warning disable CS8603 // 可能的 null 引用返回。 return((T)lake.Get(typeof(T))); #pragma warning restore CS8603 // 可能的 null 引用返回。 } catch (TypeNotFoundException) { throw new TypeNotFoundException(typeof(T)); } }
private static void RegisterBase(this ILake lake, Type target, Func <object> factory) { if (lake is null) { throw new ArgumentNullException(nameof(lake)); } if (target is null) { throw new ArgumentNullException(nameof(target)); } if (factory is null) { throw new ArgumentNullException(nameof(factory)); } lake.Register(GenerateIdOf(target), factory); }
public static ILake UniteWith(this ILake first, params ILake[] others) { if (first is null) { throw new ArgumentNullException(nameof(first)); } if (others is null) { throw new ArgumentNullException(nameof(others)); } List <ILake> tmp = new List <ILake> { [0] = first }; tmp.AddRange(others); return(new MergedLake(tmp.ToArray())); }
/// <summary> /// 注册 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="lake"></param> /// <param name="impl"></param> /// <returns></returns> public static ILake RegisterSingleton <T>(this ILake lake, Type impl) { if (lake is null) { throw new ArgumentNullException(nameof(lake)); } if (impl is null) { throw new ArgumentNullException(nameof(impl)); } var proxy = proxyBuilder.CreateProxyOf(impl); return(lake.RegisterSingleton <T>(() => { proxy.CreateInstance(); return proxy.Instance; })); }
public static object?Get(this ILake lake, Type t) { if (lake is null) { throw new ArgumentNullException(nameof(lake)); } if (t is null) { throw new ArgumentNullException(nameof(t)); } try { return(lake.GetComponent(GenerateIdByType(t))); } catch (IdNotFoundException) { throw new TypeNotFoundException(t); } }
/// <summary> /// 注册 /// </summary> /// <param name="lake"></param> /// <param name="type"></param> /// <param name="factory"></param> /// <returns></returns> public static ILake RegisterSingleton(this ILake lake, Type type, Func <object> factory) { if (lake is null) { throw new ArgumentNullException(nameof(lake)); } if (type is null) { throw new ArgumentNullException(nameof(type)); } if (factory is null) { throw new ArgumentNullException(nameof(factory)); } var lazy = new Lazy <object>(factory); return(lake.Register(type, () => lazy.Value)); }
private static void RegisterSingletonBase(this ILake lake, Type target, Func <object> factory) { if (lake is null) { throw new ArgumentNullException(nameof(lake)); } if (target is null) { throw new ArgumentNullException(nameof(target)); } if (factory is null) { throw new ArgumentNullException(nameof(factory)); } var lazy = new Lazy <object>(factory); lake.Register(GenerateIdOf(target), () => lazy.Value); }
public static bool TryGet(this ILake lake, string id, out object?value) { if (lake is null || string.IsNullOrEmpty(id)) { #pragma warning disable CS8601 // 可能的 null 引用赋值。 value = default; #pragma warning restore CS8601 // 可能的 null 引用赋值。 return(false); } try { value = lake.GetComponent(id); return(true); } catch (IdNotFoundException) { //SLogger.Warn(nameof(LakeExtension), $"Can not get component by id: {id}"); value = default; return(false); } }