示例#1
0
        protected void Application_Start()
        {

            #region View Engine
            //var engine = new 
            //ViewEngines.Engines.Clear();
            //ViewEngines.Engines.Add(engine);
            #endregion

            #region Dynamic Compile
            AsseblyLocator.Init();
            Ideative.Dinamik.CodeCompiler.DebugMode = false;
            var newdynamicModel = new dynamicModel();
            newdynamicModel.modelName = "Model1";
            newdynamicModel.modelCode = @"    public string ad { get; set; }";

            Assembly a = Ideative.Dinamik.CodeCompiler.CodeActionsInvoker(newdynamicModel);
            Debug.WriteLine(" Name : " + a.GetName().Name);
            Debug.WriteLine(" NamF : " + a.GetName());
            Debug.WriteLine(" NamF : " + a.FullName);
            #endregion

            AreaRegistration.RegisterAllAreas();

            HostingEnvironment.RegisterVirtualPathProvider(new ViewPathProvider());

            GlobalConfiguration.Configure(WebApiConfig.Register);
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);

            // ControllerBuilder.Current.SetControllerFactory(new MyControllerFActory());


        }
示例#2
0
        public static Assembly CodeActionsInvoker(dynamicModel model)
        {
            Namespace = model.modelNameSpace;
            StringBuilder stringBuilderSystem = new StringBuilder();
            StringBuilder stringBuilderCode = new StringBuilder();
            int satirNo = 0;

            // #1 Usings
            foreach (var str in Usings)
            {
                stringBuilderCode.AppendFormat("using {0};\r\n", str);
                satirNo++;
            }
            // #2 NameSpace Def.
            //stringBuilderCode.AppendFormat("namespace {0}_{1} {{", Namespace, "");
            stringBuilderCode.AppendFormat("namespace {0} {{", Namespace, "");
            stringBuilderCode.Append("\r\n");
            satirNo++;
            // #3 Class Def.
            //stringBuilderCode.AppendFormat("public static class {0}_{1}_Class {{", className,"" );
            stringBuilderCode.AppendFormat("public class {0} {{", model.modelName, "");
            stringBuilderCode.Append("\r\n");
            satirNo++;
            // #3 Debug Def.
            if (CodeCompiler.DebugMode)
            {
                model.modelCode = model.modelCode.Replace("/*break*/", "System.Diagnostics.Debugger.Break();");
            }
            stringBuilderCode.Append(model.modelCode);
            stringBuilderCode.Append("}");
            stringBuilderCode.Append("\r\n}");
            using (CSharpCodeProvider cSharpCodeProvider = new CSharpCodeProvider(new Dictionary<string, string>()
                {
                    { "CompilerVersion", "v4.0" }
                }))
            {
                CompilerParameters compilerParameter = new CompilerParameters();
                compilerParameter.OutputAssembly = "__" + Namespace;
                if (!CodeCompiler.DebugMode)
                {
                    compilerParameter.GenerateInMemory = true;
                }
                else
                {
                    compilerParameter.GenerateInMemory = false;
                    compilerParameter.IncludeDebugInformation = true;
                    compilerParameter.TempFiles = new TempFileCollection(Environment.GetEnvironmentVariable("TEMP"), true);
                }
                compilerParameter.ReferencedAssemblies.AddRange(CodeCompiler.Locations.ToArray());
                string[] str1 = new string[] { stringBuilderCode.ToString() };
                CompilerResults compilerResult = cSharpCodeProvider.CompileAssemblyFromSource(compilerParameter, str1);
                if (compilerResult.Errors.HasErrors)
                {
                    foreach (CompilerError error in compilerResult.Errors)
                    {
                        if (error.IsWarning)
                        {
                            continue;
                        }
                        int line = error.Line - satirNo - 1;
                        if (line <= 0)
                        {
                            stringBuilderSystem.AppendLine(string.Format("({0}): error {1}: {2}", "Using section", error.ErrorNumber, error.ErrorText));
                        }
                        else
                        {
                            object[] column = new object[] { line, error.Column, error.ErrorNumber, error.ErrorText };
                            stringBuilderSystem.AppendLine(string.Format("({0}:{1}): error {2}: {3}", column));
                        }
                    }
                }

                if (stringBuilderSystem.Length <= 0)
                {
                    Assembly compiledAssembly = compilerResult.CompiledAssembly;

                    return compiledAssembly;
                    var compilledType = compiledAssembly.GetType("Ideative.Web.Models.Model1");// string.Format("{0}.{1}",Namespace,className));

                    //MethodInfo[] methods = compilledType.GetMethods(BindingFlags.Static | BindingFlags.Public);
                    //methods[0].Invoke(null, null);
                    //var t = compilledType.InvokeMember("Execute",
                    //      BindingFlags.InvokeMethod | BindingFlags.Static | BindingFlags.Public,
                    //      null, null, null);
                    //Assembly compiledAssembly = compilerResult.CompiledAssembly;
                    //codeActionsInvoker.AddCompilledType(compiledAssembly.GetTypes().First<Type>());
                    //var obj = Activator.CreateInstance(compilledType);
                    new myCodeActionsInvoker().AddCompilledType(compiledAssembly.GetTypes().First<Type>());
                }
                return null;
            }
        }