0

i try to use CompileAssemblyFromSource to change 1 value at my main class. But when i compile i get error "Could not load file or assembly or one of its dependencies" and this only happens when i try change static value of other class. But if i return some output or wrote anything at Console from this FooClass than all work's fine. But how can i change value of other class?

using System;
using System.CodeDom.Compiler;
using System.Reflection;
using Microsoft.CSharp;

namespace stringToCode
{
    class Program
    {
        public static int q = 0;
        static void Main(string[] args)
        {
            string source = "namespace stringToCode { public class FooClass { public void Execute() { Program.q = 1; } } }";

            Console.WriteLine("q=" + q);
            using (var foo = new CSharpCodeProvider())
            {
                var parameters = new CompilerParameters();
                parameters.GenerateInMemory = true;

                foreach (Assembly assembly in AppDomain.CurrentDomain.GetAssemblies())
                {
                    try
                    {
                        string location = assembly.Location;
                        if (!String.IsNullOrEmpty(location))
                        {
                            parameters.ReferencedAssemblies.Add(location);
                        }
                    }
                    catch (NotSupportedException)
                    {}
                } 

                var res = foo.CompileAssemblyFromSource(parameters ,source);
                var type = res.CompiledAssembly.GetType("FooClass"); //<- here i has error
                var obj = Activator.CreateInstance(type);
                var output = type.GetMethod("Execute").Invoke(obj, new object[] { });

                Console.WriteLine("q=" + q);
                Console.ReadLine();
            }
        }
    }
}
4

1 回答 1

0

您找不到类型,因为您的代码中有编译错误。您无法以这种方式访问​​当前代码中的类。您至少应该在内存程序集中引用当前程序集。

更新

您的代码中有两个问题。首先,您必须Program公开课程。然后你应该指定输入方法的全名GetType

此代码工作正常:

using System;
using System.CodeDom.Compiler;
using System.Reflection;
using Microsoft.CSharp;

namespace stringToCode
{
    public class Program
    {
        public static int q = 0;
        static void Main(string[] args)
        {
            string source = "namespace stringToCode { public class FooClass { public void Execute() { Program.q = 1; } } }";

            Console.WriteLine("q=" + q);
            using (var foo = new CSharpCodeProvider())
            {
                var parameters = new CompilerParameters();
                parameters.GenerateInMemory = true;

                foreach (Assembly assembly in AppDomain.CurrentDomain.GetAssemblies())
                {
                    try
                    {
                        string location = assembly.Location;
                        if (!String.IsNullOrEmpty(location))
                        {
                            parameters.ReferencedAssemblies.Add(location);
                        }
                    }
                    catch (NotSupportedException)
                    {}
                } 

                var res = foo.CompileAssemblyFromSource(parameters ,source);
                var type = res.CompiledAssembly.GetType("stringToCode.FooClass"); //<- here i has error
                var obj = Activator.CreateInstance(type);
                var output = type.GetMethod("Execute").Invoke(obj, new object[] { });

                Console.WriteLine("q=" + q);
                Console.ReadLine();
            }
        }
    }
}
于 2015-08-23T10:42:03.153 回答