コード例 #1
0
        public async static Task <IModule> LoadModuleAsync(this ControllerBase ctrl, Type type, params object[] args)
        {
            if (type == null)
            {
                return(null);
            }

            var r = Activator.CreateInstance(type) as IModule;

            Task t = type.CallMethod(r, () => type.GetMethods().Where(it => it.Name == "Init" && it.ReturnType == typeof(Task)), (mtype, atype) =>
            {
                if (typeof(ControllerBase).IsAssignableFrom(mtype) && !typeof(ControllerBase).IsAssignableFrom(atype))
                {
                    return(ctrl);
                }

                return(null);
            }, args) as Task;

            if (t != null)
            {
                await t;
            }

            if (r != null)
            {
                WebExModuleExtensions.RegisterModule(ctrl.ControllerContext.RequestContext.HttpContext.Items, r);
            }

            return(r);
        }
コード例 #2
0
        public static IModule LoadModule(IDictionary storage, Type type, params object[] args)
        {
            if (type == null)
            {
                return(null);
            }

            var r = type.CreateInstance(null, args) as IModule;

            if (r != null)
            {
                WebExModuleExtensions.RegisterModule(storage, r);
            }

            return(r);
        }
コード例 #3
0
        public static IModule LoadModule(this ControllerBase ctrl, Type type, params object[] args)
        {
            if (type == null)
            {
                return(null);
            }

            var r = type.CreateInstance((mtype, atype) =>
            {
                if (typeof(ControllerBase).IsAssignableFrom(mtype) && !typeof(ControllerBase).IsAssignableFrom(atype))
                {
                    return(ctrl);
                }

                return(null);
            }, args) as IModule;

            if (r != null)
            {
                WebExModuleExtensions.RegisterModule(ctrl.ControllerContext.RequestContext.HttpContext.Items, r);
            }

            return(r);
        }
コード例 #4
0
        public static IModule LoadModule(this ControllerBase ctrl, Type type, params object[] args)
        {
            if (type == null)
            {
                return(null);
            }

            var r = type.CreateInstance((mtype, atype) =>
            {
                if (typeof(ControllerBase).IsAssignableFrom(mtype) && !typeof(ControllerBase).IsAssignableFrom(atype))
                {
                    return(ctrl);
                }

                return(null);
            }, args) as IModule;

            //foreach (var method in type.GetConstructors())
            //{
            //    var methodParams = method.GetParameters();
            //    if (methodParams.Count() == 0)
            //    {
            //        r = Activator.CreateInstance(type) as IModule;
            //    }
            //    else
            //    {
            //        var params2Call = new object[methodParams.Count()];
            //        int j = 0;
            //        for (int i = 0; i < params2Call.Length; i++)
            //        {
            //            var mtype = methodParams[i].ParameterType;
            //            object arg = null;
            //            if (args != null && args.Length > j)
            //                arg = args[j];

            //            if (arg == null)
            //            {
            //                if (typeof(ControllerBase).IsAssignableFrom(mtype))
            //                    params2Call[i] = ctrl;

            //                if (args != null && args.Length > j)
            //                    j++;
            //            }
            //            else
            //            {
            //                if (mtype == arg.GetType())
            //                {
            //                    params2Call[i] = arg;
            //                    j++;
            //                }
            //                else if (typeof(ControllerBase).IsAssignableFrom(mtype) && !typeof(ControllerBase).IsAssignableFrom(arg.GetType()))
            //                {
            //                    params2Call[i] = ctrl;
            //                }
            //                else
            //                {
            //                    try
            //                    {
            //                        params2Call[i] = Convert.ChangeType(arg, mtype);
            //                        j++;
            //                    }
            //                    catch(Exception ex)
            //                    {
            //                        //eat an exception
            //                    }
            //                }
            //            }
            //        }
            //        r = Activator.CreateInstance(type, params2Call) as IModule;
            //    }
            //}

            if (r != null)
            {
                WebExModuleExtensions.RegisterModule(ctrl.ControllerContext.RequestContext.HttpContext.Items, r);
            }

            return(r);
        }