将对象传递给Dynamic Assembly

时间:2015-09-01 10:20:25

标签: c# .net codedom

我有一些类型的对象需要传递给动态程序集。但我的类型无法识别,我收到编译错误

这是我编译的代码

using System.Linq;
using System.Text;
using System.Collections.Generic;
using SkillBuilder.AutoGens.Libs;

namespace TempNs
{
    public class MyClass
    {
        public MyClass()
        {}

        public Autozen ag; 
        //do stuff
    }
}

上面的代码抛出了type Autozen could not be found. Are you missing an Assembly reference?的错误。类型位于SkillBuilder.AutoGens.Libs

这就是我编译它的方式。

public static Assembly Compile(string sourceCode)
    {
        var provider_options = new Dictionary<string, string>
                     {
                         {"CompilerVersion","v4.0"}
                     };
        CodeDomProvider cpd = new CSharpCodeProvider(provider_options);
        var cp = new CompilerParameters();
        cp.ReferencedAssemblies.Add("System.dll");
        cp.ReferencedAssemblies.Add("System.Core.Dll");

        // True - memory generation, false - external file generation
        cp.GenerateInMemory = true;
        cp.GenerateExecutable = false;
        CompilerResults cr = cpd.CompileAssemblyFromSource(cp, sourceCode);

        if (cr.Errors.HasErrors)
        {
            StringBuilder sb = new StringBuilder();

            foreach (CompilerError error in cr.Errors)
            {
                sb.AppendLine(String.Format("Error ({0}): {1}", error.ErrorNumber, error.ErrorText));
            }

            throw new InvalidOperationException(sb.ToString());
        }

        return cr.CompiledAssembly;
    }

我希望能够分配&#34; ag&#34;的值。通过调用应用程序。 如何在编译期间使Autozen可用?还有其他办法吗?

编辑: 代码由EXE生成和编译。编译后的输出将再次由同一EXE使用。 谢谢你的帮助。

编辑: 将Assembly.GetExecutingAssembly()。Location添加到引用的程序集集合后,我得到System.Core.dll缺少引用错误。由于我已经包含了CompilerVersion,它应该从GAC中获取程序集。那我为什么还会收到这个错误?

0 个答案:

没有答案
相关问题