在运行时动态生成 DLL 程序集 [英] Generating DLL assembly dynamically at run time

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

问题描述

目前我有一些动态生成的代码.换句话说,程序动态创建了一个 C# .cs 文件,目的是将这个 C# 文件包含在另一个项目中.

Currently I have some code that is being generated dynamically. In other words, a C# .cs file is created dynamically by the program, and the intention is to include this C# file in another project.

挑战在于我想生成一个 .DLL 文件而不是生成一个 C# .cs 文件,以便它可以被任何类型的 .NET 应用程序(不仅仅是 C#)引用,因此更有用.

The challenge is that I would like to generate a .DLL file instead of generating a C# .cs file so that it could be referenced by any kind of .NET application (not only C#) and therefore be more useful.

推荐答案

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

CSharpCodeProvider codeProvider = new CSharpCodeProvider();
ICodeCompiler icc = codeProvider.CreateCompiler();
System.CodeDom.Compiler.CompilerParameters parameters = new CompilerParameters();
parameters.GenerateExecutable = false;
parameters.OutputAssembly = "AutoGen.dll";
CompilerResults results = icc.CompileAssemblyFromSource(parameters, yourCodeAsString);

改编自 http://support.microsoft.com/kb/304655

这篇关于在运行时动态生成 DLL 程序集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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