Ejemplo n.º 1
0
        private void ExecuteInMemoryAssembly(HostedPlugin plugin, string input)
        {
            var context = new CollectibleAssemblyContext();

            using (var ms = new MemoryStream())
            {
                var compilation = CSharpCompilation.Create(plugin.Name, new[] { CSharpSyntaxTree.ParseText(plugin.Code) },
                                                           new[]
                {
                    MetadataReference.CreateFromFile(typeof(object).GetTypeInfo().Assembly.Location),
                    MetadataReference.CreateFromFile(typeof(Console).GetTypeInfo().Assembly.Location),
                    MetadataReference.CreateFromFile(SystemRuntime.Location),
                },
                                                           new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary));

                var cr = compilation.Emit(ms);
                ms.Seek(0, SeekOrigin.Begin);
                var assembly = context.LoadFromStream(ms);

                var type          = assembly.GetType("Plugin");
                var executeMethod = type.GetMethod("Execute");

                var instance = Activator.CreateInstance(type);

                var dic = PluginResponses.GetOrCreate(plugin.Name);

                dic.Add(executeMethod.Invoke(instance, new object[] { input }).ToString());
            }

            context.Unload();
        }
Ejemplo n.º 2
0
        private void ExecuteAssembly(HostedPlugin plugin, string input)
        {
            var context      = new CollectibleAssemblyContext();
            var assemblyPath = Path.Combine(plugin.FilePath);

            using (var fs = new FileStream(assemblyPath, FileMode.Open, FileAccess.Read))
            {
                var assembly = context.LoadFromStream(fs);

                var type          = assembly.GetType("PluginSystem.Plugin");
                var executeMethod = type.GetMethod("Execute");

                var instance = Activator.CreateInstance(type);

                var dic = PluginResponses.GetOrCreate(plugin.Name);

                dic.Add(executeMethod.Invoke(instance, new object[] { input }).ToString());
            }

            context.Unload();
        }