예제 #1
0
        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;
            });
        }
예제 #2
0
 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;
         }
     }));
 }
예제 #3
0
        public void RegisterTest()
        {
            ILake factory = APIFactory.Instance;

            factory.Register <string>(() => "test");
            Assert.IsTrue(factory.Get <string>() == "test");
        }
예제 #4
0
        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 }));
        }
예제 #5
0
 static LakeProvider()
 {
     if (Lake == null)
     {
         Lake = null;
     }
 }
예제 #6
0
        /// <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));
        }
예제 #7
0
        /// <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)));
        }
예제 #8
0
        /// <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)));
        }
예제 #9
0
        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()));
        }
예제 #10
0
            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;
            }
예제 #11
0
        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>());
        }
예제 #12
0
        public void GetNotExistTest()
        {
            ILake     factory = APIFactory.Instance;
            Exception ex      = null;

            try { factory.Get <string>(); }
            catch (Exception e)
            {
                ex = e;
            }

            Assert.IsTrue(ex != null);
        }
예제 #13
0
 /// <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);
     }
 }
예제 #14
0
 /// <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);
     }
 }
예제 #15
0
        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);
        }
예제 #16
0
        /// <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;
            }));
        }
예제 #17
0
        /// <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));
        }
예제 #18
0
        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(' ', ','));
        }
예제 #19
0
        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 !;
예제 #20
0
        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));
        }
예제 #21
0
        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());
        }
예제 #22
0
        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 !;
예제 #23
0
        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));
            }
        }
예제 #24
0
        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);
        }
예제 #25
0
        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()));
        }
예제 #26
0
        /// <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;
            }));
        }
예제 #27
0
        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);
            }
        }
예제 #28
0
        /// <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));
        }
예제 #29
0
        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);
        }
예제 #30
0
        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);
            }
        }