【问题标题】:I can't change other Class var value with CompileAssemblyFromSource我无法使用 CompileAssemblyFromSource 更改其他 Class var 值
【发布时间】:2015-08-23 10:33:46
【问题描述】:

我尝试使用 CompileAssemblyFromSource 在我的主类中更改 1 个值。 但是当我编译时出现错误“无法加载文件或程序集或其依赖项之一”,这只发生在我尝试更改其他类的静态值时。但是,如果我从这个 FooClass 返回一些输出或在控制台上写任何东西,那么一切都很好。但是如何更改其他类的值?

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();
            }
        }
    }
}

【问题讨论】:

  • 试试GetType("stringToCode.FooClass")。此外,Program 是内部的,除非您向主程序集添加一些属性或将其公开,否则它将不起作用。
  • 这有帮助!谢谢!

标签: c# compileassemblyfromsource


【解决方案1】:

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

更新

您的代码中有两个问题。首先,您必须公开课程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();
            }
        }
    }
}

【讨论】:

  • 请检查我的代码。已经引用了所有程序集,但这无济于事。在同一个地方仍然有同样的错误。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多