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

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

问题描述

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



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

解决方案

 使用System.CodeDom.Compiler; 
使用System.Diagnostics;
使用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


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.

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);

Adapted from http://support.microsoft.com/kb/304655

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

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