Parse() public static method

public static Parse ( string source ) : CSharpControllerFile
source string
return CSharpControllerFile
Exemplo n.º 1
0
        public IController Create(RequestContext requestContext, Type controllerType)
        {
            CompilerSettings settings = new CompilerSettings();
            Report           report   = new Report(new ConsoleReportPrinter());
            Evaluator        eval     = new Evaluator(settings, report);

            object instance        = null;
            bool   instanceCreated = false;

            eval.ReferenceAssembly(typeof(Controller).Assembly);

            foreach (Assembly assembly in assemblies)
            {
                eval.ReferenceAssembly(assembly);
            }

            string controllerName = GetControllerName(requestContext, controllerType);
            string path           = pathProvider.GetPath(requestContext, controllerName);
            CSharpControllerFile controllerFile = CSharpControllerFile.Parse(File.ReadAllText(path));

            eval.Run(controllerFile.ClassSource);
            eval.Evaluate("new " + controllerName + "();", out instance, out instanceCreated);

            return((IController)instance);
        }
Exemplo n.º 2
0
        protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType)
        {
            string controllerName = GetControllerName(requestContext, controllerType);
            string path           = pathProvider.GetPath(requestContext, controllerName);
            CSharpControllerFile controllerFile = CSharpControllerFile.Parse(File.ReadAllText(path));

            var parameters = new CompilerParameters();

            addReferencedAssemblies(parameters);

            var codeProvider = new CSharpCodeProvider().CompileAssemblyFromSource(parameters, controllerFile.ClassSource);

            if (denpedencyInjector == null)
            {
                object controllerInstance = codeProvider.CompiledAssembly.CreateInstance(controllerFile.FullClassName);
                return((IController)controllerInstance ?? base.GetControllerInstance(requestContext, controllerType));
            }
            else
            {
                return(denpedencyInjector.CreateControllerInstance(codeProvider.CompiledAssembly.GetType(controllerFile.FullClassName)));
            }
        }