Пример #1
0
        public async Task <IActionResult> PerformPreGenerationChecks([FromBody] ScheduleCreatorDto scheduleCreator)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }

            string assemblyPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase)
                                  .Substring(6) + Path.DirectorySeparatorChar + scheduleCreator.AssemblyDirName;


            if (!scheduleCreator.Date.HasValue)
            {
                return(BadRequest());
            }

            ISpeciesOriginator speciesOriginator = DynamicFactory.GetSpeciesOriginator(assemblyPath, scheduleCreator.Date.Value, scheduleCreator.Args);

            List <Requirement> requirements = DynamicFactory.GetRequirements(assemblyPath, scheduleCreator.Date.Value, scheduleCreator.Args);

            await speciesOriginator.SetDataFromCustomApi();

            var messenger = speciesOriginator.PerformPreGenerationChecks();

            return(Ok(messenger.Messages));
        }
Пример #2
0
        /// <summary>
        /// 获取实体Grain代理
        /// </summary>
        /// <param name="grain">Grain接口</param>
        /// <returns>实体Grain代理</returns>
        public static T Fetch(TGrainInterface grain)
        {
            T result = DynamicFactory.Create <T>();

            result.Grain = grain;
            return(result);
        }
Пример #3
0
        public static Dynamic GetValue(Type type, string memberName, object target)
        {
            object result = null;

            if (type.IsCOMObject)
            {
                const BindingFlags flags = BindingFlags.Public | BindingFlags.Static |
                                           BindingFlags.Instance | BindingFlags.GetField |
                                           BindingFlags.GetProperty | BindingFlags.OptionalParamBinding;

                result = type.InvokeMember(memberName, flags, null, target, null);
            }
            else
            {
                MemberInfo member = GetValueMember(type, memberName);
                switch (member.MemberType)
                {
                case MemberTypes.Field:
                    var field = (FieldInfo)member;
                    result = field.GetValue(target);
                    break;

                case MemberTypes.Property:
                    var property = (PropertyInfo)member;
                    if (!property.CanRead)
                    {
                        throw new InvalidOperationException(Resources.CannotReadProperty);
                    }
                    result = property.GetValue(target, null);
                    break;
                }
            }

            return(DynamicFactory.CreateDynamic(result));
        }
Пример #4
0
    public static void Main()
    {
        TestClass tc1 = (TestClass)DynamicFactory.Create("TestClass", new ImplicitTest(15.5));
        TestClass tc2 = (TestClass)DynamicFactory.Create("TestClass", 5);

        Console.WriteLine(tc1.Val);
        Console.WriteLine(tc2.Val);
    }
Пример #5
0
        public void CtorTest()
        {
            var instance = DynamicFactory.CreateInstance <CtorClass>(1);

            Assert.Equal(1, instance.X);

            instance = DynamicFactory.CreateInstance <CtorClass>(1, 2);
            Assert.Equal(2, instance.Y);

            instance = DynamicFactory.CreateInstance <CtorClass>(1, 2, 3);
            Assert.Equal(3, instance.Z);
        }
Пример #6
0
        public async Task <IActionResult> GetSchedule([FromBody] ScheduleCreatorDto scheduleCreator)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }

            /*//string assemblyPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase)
             *                        .Substring(6) + Path.DirectorySeparatorChar + scheduleCreator.AssemblyDirName;*/

            string assemblyPath = Path.Combine(_environment.ContentRootPath, scheduleCreator.AssemblyDirName);

            if (!scheduleCreator.Date.HasValue)
            {
                return(BadRequest());
            }

            ISpeciesOriginator speciesOriginator = DynamicFactory.GetSpeciesOriginator(assemblyPath, scheduleCreator.Date.Value, scheduleCreator.Args);

            List <Requirement> requirements = DynamicFactory.GetRequirements(assemblyPath, scheduleCreator.Date.Value, scheduleCreator.Args);

            await speciesOriginator.SetDataFromCustomApi();

            foreach (var requirement in requirements)
            {
                await requirement.SetDataFromRemoteApi();
            }

            Population population = new Population(new CostCalculator(requirements), speciesOriginator);

            population.Initialize();

            population.CalculateGeneralCost();

            do
            {
                population.PerformNaturalSelection();

                population.CalculateGeneralCost();
            } while (population.NumberOfGenerations < 100);

            IAllel[,] solution = population.BestSolution.Dna;

            Dictionary <DateTime, object> data = speciesOriginator.CustomFormatSolution(solution, scheduleCreator.Date.Value);

            Dictionary <string, object> dataToReturn = Converter.ConvertDateTimeToString(data);

            return(Ok(dataToReturn));
        }
Пример #7
0
            public object CreateAny(object value)
            {
                if (value == null)
                {
                    return(null);
                }
                var mp = TypeMapper.Create(value.GetType());
                var d  = this._ctor.GetParameters().ToDictionary(s => s.Name, s => (object)null);

                foreach (var p in mp.Properties)
                {
                    d[p.Name] = p.GetValue(value);
                }
                return(DynamicFactory.CreateConstructorHandler(_ctor)(d.Values.ToArray()));
            }
Пример #8
0
        /// <summary>
        /// Invokes a scripted function from user code.
        /// </summary>
        /// <param name="function">The name of the function to be invoked</param>
        /// <param name="args">Arguments to be passed to the function</param>
        /// <returns>The value returned by the function itself</returns>
        public Dynamic Invoke(string function, params object[] args)
        {
            var literals = new Expression[args.Length];

            for (int i = 0; i < args.Length; ++i)
            {
                literals[i] = new Literal(DynamicFactory.CreateDynamic(args[i]));
            }

            var call = new FunctionCall(function, literals);

            RuntimeServices.Interpreter = interpreter;
            call.AcceptCompiler(interpreter);

            return(interpreter.ReturnedValue);
        }
Пример #9
0
        /// <summary>
        /// Invokes a method from the given object with the given arguments.
        /// </summary>
        /// <param name="method">The method's name</param>
        /// <param name="caller">The object from which to call a method</param>
        /// <param name="args">The arguments to pass to the method</param>
        /// <returns>The value returned by the method</returns>
        public static Dynamic Invoke(string method, Dynamic caller, params object[] args)
        {
            Expression callingExpr = new Literal(caller);

            var literals = new Expression[args.Length];

            for (int i = 0; i < args.Length; ++i)
            {
                literals[i] = new Literal(DynamicFactory.CreateDynamic(args[i]));
            }

            var call = new MethodCall(callingExpr, method, literals);

            call.AcceptCompiler(Interpreter);

            return(Interpreter.ReturnedValue);
        }
Пример #10
0
        /// <summary>
        /// Invokes a static method with the given arguments.
        /// </summary>
        /// <param name="method">The method's name</param>
        /// <param name="klass">The class from which to call a method</param>
        /// <param name="args">The arguments to pass to the method</param>
        /// <returns>The value returned by the method</returns>
        public static Dynamic Invoke(string method, Class klass, params object[] args)
        {
            var name = new QualifiedName(klass.Name, method);

            var literals = new Expression[args.Length];

            for (int i = 0; i < args.Length; ++i)
            {
                literals[i] = new Literal(DynamicFactory.CreateDynamic(args[i]));
            }

            var call = new StaticMethodCall(name, literals);

            call.AcceptCompiler(Interpreter);

            return(Interpreter.ReturnedValue);
        }
    public static void Main()
    {
        try
        {
            TestClass tc1 = (TestClass)DynamicFactory.Create("TestClass", new ImplicitTest(15.5));
            TestClass tc2 = (TestClass)DynamicFactory.Create("TestClass", 5);
            TestClass tc3 = (TestClass)DynamicFactory.Create("TestClass", new ImplicitTest(15.5), new ImplicitTest(10.0));

            Console.WriteLine(tc1.Val);
            Console.WriteLine(tc2.Val);
            Console.WriteLine(tc3.Val);
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex);
        }
    }
Пример #12
0
        private static IDbEngineProvider CreateProvider(string provider, string connectionString)
        {
            var type = RegisterProviders.GetOrAdd(provider, GetProviderType);

            if (type == null)
            {
                RegisterProviders.TryRemove(provider, out type);
                throw new NotSupportedException($"当前运行环境找不到名称为“{provider}”的数据库提供程序。");
            }

            var ctor = type.GetConstructor(new Type[] { DefineTypes.String });

            if (ctor == null)
            {
                throw new NotSupportedException($"当前运行环境名称为“{provider}”的数据库提供程序找不到“{type.FullName}(connectionString)” 的构造函数。");
            }
            return((IDbEngineProvider)DynamicFactory.CreateConstructorHandler(ctor)(connectionString));
        }
Пример #13
0
        internal void CreateDiffEditor(IAnkhServiceProvider context, AnkhDiffArgs args)
        {
            Context = context;

            DynamicFactory.CreateEditor(args.BaseFile, this);
            OnFrameCreated(EventArgs.Empty);

            Collection <string> A, B;

            GetFileLines(args.BaseFile, args.MineFile, out A, out B);
            TextDiff   Diff   = new TextDiff(HashType.HashCode, false, false);
            EditScript Script = Diff.Execute(A, B);

            string strCaptionA = args.BaseTitle ?? Path.GetFileName(args.BaseFile);
            string strCaptionB = args.MineTitle ?? Path.GetFileName(args.MineFile);

            diffControl1.SetData(A, B, Script, strCaptionA, strCaptionB);
        }
Пример #14
0
        public void StructTest()
        {
            var box = DynamicFactory.CreateInstance(typeof(MyStruct));

            DynamicInstance di = new DynamicInstance(box);

            di.Set("Field", 6);
            di.Set("Property", "AAA");
            di.Set("ChildStruct", new ChildStruct()
            {
                Field = 12
            });
            var str = (MyStruct)box;

            Assert.Equal(6, str.Field);
            Assert.Equal("AAA", str.Property);
            Assert.Equal(12, str.ChildStruct.Field);

            di = new DynamicInstance(str);
            Assert.Equal(6, di.Get("Field"));
            Assert.Equal("AAA", di.Get("Property"));
            Assert.Equal(12, ((ChildStruct)di.Get("ChildStruct")).Field);
        }
Пример #15
0
            /// <summary>
            /// 创建一个模拟的 MVC 控制器。
            /// </summary>
            /// <typeparam name="TController">控制的数据类型。</typeparam>
            /// <param name="user">当前已授权的登录用户。</param>
            /// <param name="mockFactoryCallback">模拟的执行器工厂回调函数。</param>
            /// <returns>返回一个控制器的实例。</returns>
            public static TController Create <TController>(object user, Action <MockExecutorFactory> mockFactoryCallback = null)
                where TController : Web.Mvc.Controller, new()
            {
                var httpRequest  = new HttpRequest(string.Empty, "http://www.aoite.com/", string.Empty);
                var stringWriter = new IO.StringWriter();
                var httpResponce = new HttpResponse(stringWriter);
                var httpContext  = new HttpContext(httpRequest, httpResponce);

                var sessionContainer = new SessionState.HttpSessionStateContainer("id", new SessionState.SessionStateItemCollection(),
                                                                                  new HttpStaticObjectsCollection(), 10, true,
                                                                                  HttpCookieMode.AutoDetect,
                                                                                  SessionState.SessionStateMode.InProc, false);

                httpContext.Items["AspSession"] = typeof(SessionState.HttpSessionState).GetConstructor(
                    BindingFlags.NonPublic | BindingFlags.Instance,
                    null, CallingConventions.Standard,
                    new[] { typeof(SessionState.HttpSessionStateContainer) },
                    null)
                                                  .Invoke(new object[] { sessionContainer });
                HttpContext.Current = httpContext;

                var appFactoryType = typeof(HttpContext).Assembly.GetType("System.Web.HttpApplicationFactory");

                object appFactory = DynamicFactory.CreateFieldGetter(appFactoryType.GetField("_theApplicationFactory", BindingFlags.NonPublic | BindingFlags.Static))(null);

                DynamicFactory.CreateFieldSetter(appFactoryType.GetField("_state", BindingFlags.NonPublic | BindingFlags.Instance))(appFactory, HttpContext.Current.Application);

                var identityStore = new MockSessionIdentityStore(user);
                var container     = ServiceFactory.CreateContainer(identityStore, mockFactoryCallback);

                container.Add <IIdentityStore>(identityStore);
                Webx.Container = container;
                TController c = new TController();

                c.ControllerContext = new Web.Mvc.ControllerContext(new Routing.RequestContext(new HttpContextWrapper(httpContext), new Routing.RouteData()), c);
                return(c);
            }
 public static void ForTitanic()
 {
     DynamicFactory <Titanic> .Create("Titanic")
     .ShouldBeType(typeof(Titanic));
 }
Пример #17
0
 public ExcelConfigSet(DynamicFactory fact, string file_name, string protocol_name)
 {
     factory  = fact;
     fileName = file_name;
     protocol = protocol_name;
 }
Пример #18
0
 public TView Create(DynamicEntityValue item) =>
 (TView)DynamicFactory.CreateConstructorHandler(_ctor)(item.NameValues.Values.ToArray());
Пример #19
0
 public ServiceWithNotRegisteredLazyParameter(DynamicFactory <NotRegisteredService> parameter)
 {
     Parameter = parameter;
 }