Exemplo n.º 1
0
        public static dynamic ExecuteTest(string text, Action <ICompiler <SyntaxToken, SyntaxNode, SemanticModel> > config)
        {
            var compilation = new Roslyn.Compilation(null, null);
            var injector    = new CompositeInjector <SyntaxToken, SyntaxNode, SemanticModel>(new[] {
                _main,
                new DelegateInjector <SyntaxToken, SyntaxNode, SemanticModel>(compiler => config(compiler))
            });

            compilation.addDocument("test", text, injector);

            Assembly assembly = compilation.build();

            if (assembly == null)
            {
                //debug
                StringBuilder errorLines = new StringBuilder();
                foreach (var error in compilation.errors())
                {
                    errorLines.AppendLine(error.ToString());
                }

                var errorString = errorLines.ToString();
                return(null);
            }

            Type testtype = assembly.GetType("testclass");
            //var method = console.GetMethod("test", BindingFlags.Static);

            var result = new Dictionary <string, object>();

            testtype.InvokeMember("test", BindingFlags.InvokeMethod | BindingFlags.Static | BindingFlags.NonPublic, null, null, new object[] { result });

            var xo  = new ExpandoObject();
            var xod = xo as IDictionary <string, object>;

            foreach (var kp in result)
            {
                xod.Add(kp.Key, kp.Value);
            }

            return(xo);
        }
Exemplo n.º 2
0
        public static Node Build(string text, out IEnumerable <Diagnostic> errors, int threads = 1)
        {
            errors = null;

            var compilation = new Roslyn.Compilation(null);
            var injector    = new CompositeInjector <SyntaxToken, SyntaxNode, SemanticModel>(new[]
            {
                new DelegateInjector <SyntaxToken, SyntaxNode, SemanticModel>(compiler => compiler
                                                                              .Environment()
                                                                              .dependency <console>("Excess.Compiler.Tests.TestRuntime")
                                                                              //.dependency<object>(new[] {
                                                                              //    "System",
                                                                              //    "System.Collections",
                                                                              //    "System.Collections.Generic" })
                                                                              .dependency(new[] {
                    "System.Threading",
                    "System.Threading.Tasks",
                    "System.Diagnostics",
                })),

                new DelegateInjector <SyntaxToken, SyntaxNode, SemanticModel>(compiler =>
                                                                              Extensions
                                                                              .Concurrent.Extension
                                                                              .Apply(compiler))
            });

            compilation.addDocument("concurrent-test", text, injector);

            Assembly assembly = compilation.build();

            if (assembly == null)
            {
                errors = compilation.errors();

                //debug
                StringBuilder errorLines = new StringBuilder();
                foreach (var error in errors)
                {
                    errorLines.AppendLine(error.ToString());
                }

                var errorString = errorLines.ToString();
                return(null);
            }

            var exportTypes = new Dictionary <string, Spawner>();

            foreach (var type in assembly.GetTypes())
            {
                if (type.BaseType != typeof(ConcurrentObject))
                {
                    continue;
                }

                var useParameterLess = type.GetConstructors().Length == 0;
                if (!useParameterLess)
                {
                    useParameterLess = type.GetConstructor(new Type[] { }) != null;
                }

                var typeName = type.ToString();
                exportTypes[typeName] = (args) =>
                {
                    if (useParameterLess)
                    {
                        return((ConcurrentObject)Activator.CreateInstance(type));
                    }

                    var ctor = type.GetConstructor(args
                                                   .Select(arg => arg.GetType())
                                                   .ToArray());

                    if (ctor != null)
                    {
                        return((ConcurrentObject)ctor.Invoke(args));
                    }

                    throw new InvalidOperationException("unable to find a constructor");
                };
            }

            return(new Node(threads, exportTypes));
        }