C# - 引用在动态生成的程序集的类型 [英] C# - Referencing a type in a dynamically generated assembly

查看:910
本文介绍了C# - 引用在动态生成的程序集的类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找出是否有可能当你动态生成组件,以引用先前动态生成的程序集的类型

I'm trying to figure out if it's possible when you are dynamically generating assemblies, to reference a type in a previously dynamically generated assembly.

例如:

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

CodeDomProvider provider = new CSharpCodeProvider();
CompilerParameters parameters = new CompilerParameters();

parameters.GenerateInMemory = true;

CompilerResults results = provider.CompileAssemblyFromSource(parameters, @"
namespace Dynamic
{
    public class A
    {
    }
}
");

Assembly assem = results.CompiledAssembly;

CodeDomProvider provider2 = new CSharpCodeProvider();
CompilerParameters parameters2 = new CompilerParameters();

parameters2.ReferencedAssemblies.Add(assem.FullName);
parameters2.GenerateInMemory = true;

CompilerResults results2 = provider2.CompileAssemblyFromSource(parameters2, @"
namespace Dynamic
{
    public class B : A
    {
    }
}
");

if (results2.Errors.HasErrors)
{
    foreach (CompilerError error in results2.Errors)
    {
        Console.WriteLine(error.ErrorText);
    }
}
else
{
    Assembly assem2 = results2.CompiledAssembly;
}

这代码打印在控制台上的以下内容:的类型或命名空间名称'A'找不到(是否缺少using指令或程序集引用?)

This code prints the following on the console: The type or namespace name 'A' could not be found (are you missing a using directive or an assembly reference?)

我试过这许多不同的方式,但似乎没有奏效。我缺少的东西吗?这甚至可能

I've tried it lots of different ways, but nothing seems to be working. Am I missing something? Is this even possible?

编辑:在代码修复bug提供了这个错误,而不是:
元数据文件'l0livsmn,版本= 0.0.0.0,文化=中立,公钥=空'找不到

Fixing the bug in the code provides this error instead: Metadata file 'l0livsmn, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' could not be found

EDIT2:一个方面说明的位,但改变GenerateInMemory到假的,做 parameters2.ReferencedAssemblies.Add(assem.Location); 将导致其正确编译,但我非常喜欢引用一个是直接在内存中的程序集而不是输出的临时文件。

Bit of a side note, but changing GenerateInMemory to false, and doing parameters2.ReferencedAssemblies.Add(assem.Location); will cause it to compile correctly, but I'd greatly prefer to reference the assembly that is directly in memory rather than outputting temporary files.

推荐答案

我认为,在

CompilerResults results2 = provider2.CompileAssemblyFromSource(parameters, @"
namespace Dynamic
{
    public class B : A
    {
    }
}
");

您想通过 parameters2 ,而不是参数

我发现这样做的方式,你不需要编译内存中的第一个,如果你不这样做,它会创建一个dll了本次大会在temp目录,再加上,在您的来电

I found the way to do it, you need NOT to compile the first one in memory, if you don't do that, it will create a dll for this assembly in your temp directory, plus, in your call to

ReferencedAssemblies.Add() 

您不要通过集名称,您通过装配路径,看看这个代码,它应该工作得很好:

you dont pass the assembly name, you pass the assembly path, take a look at this code, it should work flawlessly :

        CodeDomProvider provider = new CSharpCodeProvider();
        CompilerParameters parameters = new CompilerParameters();            

        CompilerResults results = provider.CompileAssemblyFromSource(parameters, @"
            namespace Dynamic
            {
                public class A
                {
                }
            }
            ");

        Assembly assem = results.CompiledAssembly;

        CodeDomProvider provider2 = new CSharpCodeProvider();
        CompilerParameters parameters2 = new CompilerParameters();

        parameters2.ReferencedAssemblies.Add(assem.Location);
        parameters2.GenerateInMemory = true;

        CompilerResults results2 = provider2.CompileAssemblyFromSource(parameters2, @"
            namespace Dynamic
            {
                public class B : A
                {
                }
            }
            ");

        if (results2.Errors.HasErrors)
        {
            foreach (CompilerError error in results2.Errors)
            {
                Console.WriteLine(error.ErrorText);
            }
        }
        else
        {
            Assembly assem2 = results2.CompiledAssembly;
        }

这篇关于C# - 引用在动态生成的程序集的类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆