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

查看:19
本文介绍了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?

修复代码中的错误会提供此错误:找不到元数据文件l0livsmn,版本=0.0.0.0,文化=中性,PublicKeyToken=null"

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

旁注,但将 GenerateInMemory 更改为 false,并执行 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,而不是parameters.

我找到了这样做的方法,你不需要在内存中编译第一个,如果你不这样做,它会在你的临时目录中为这个程序集创建一个 dll,另外,在你调用

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天全站免登陆