首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我无法用CompileAssemblyFromSource更改其他类var值

我无法用CompileAssemblyFromSource更改其他类var值
EN

Stack Overflow用户
提问于 2015-08-23 10:33:46
回答 1查看 194关注 0票数 0

我尝试使用CompileAssemblyFromSource来更改主类中的1值。但是,当我编译时,我会得到错误“无法加载文件或程序集或它的依赖项之一”,只有当我尝试更改其他类的静态值时才会发生这种情况。但是如果我从这个FooClass返回一些输出或在控制台上写任何东西,那么所有的工作都是可以的。但是如何改变其他类的值呢?

代码语言:javascript
复制
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();
            }
        }
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-08-23 10:42:03

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

更新

代码中有两个问题。首先,您必须使类Program公开。然后,应该在GetType方法中指定类型的全名。

此代码运行良好:

代码语言:javascript
复制
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();
            }
        }
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32165703

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档