CSharpCodeProvider:引用其他生成的“InMemory” assembl [英] CSharpCodeProvider: referencing other generated "InMemory" assembl
问题描述
我正在使用CSharpCodeProvider构建一些程序集@ runtime,它们从未保存为文件(cp.GenerateInMemory = true;)。
生成的程序集在分层上依赖于彼此所以我
生成底部首先是程序集。
如何向另一个先前加载(生成)的程序集添加依赖项?
如果CompilerParameters.ReferencedAssemblies,我会很高兴.Add可以带一个
System.Reflection.Assembly引用作为参数。
一个可能的解决方案是生成dll文件并引用它们但我是/>
就像我的应用程序退出时没有任何文件要清理的想法。
任何建议都赞赏。
/ Jan
I''m using the CSharpCodeProvider to buils some assemblies @ runtime which are
never saved as files (cp.GenerateInMemory = true;).
The generated assemblies are hierachically dependent on each other so I
generate the "bottom" assemblies first.
How do I add a dependency to another previously loaded (generated) assembly?
I would be happy if CompilerParameters.ReferencedAssemblies.Add could take a
System.Reflection.Assembly reference as parameter.
A possible solution would be to generate dll files and reference them but I
like the idea of not having any files to cleanup when my application exits.
Any suggestions appreciated.
/Jan
推荐答案
Jan写道:
Jan wrote:
我正在使用CSharpCodeProvider构建一些程序集@ runtime
永远不会保存为文件(cp.GenerateInMemory = true;)。
生成的程序集在层次上彼此依赖,因此我生成底部。首先是程序集。
如何向另一个先前加载(生成)的程序集添加依赖项?
如果CompilerParameters.ReferencedAssemblies.Add可以<将System.Reflection.Assembly引用作为参数。
一个可能的解决方案是生成dll文件并引用它们
但我喜欢没有任何文件要清理的想法当我的
应用程序退出时。
I''m using the CSharpCodeProvider to buils some assemblies @ runtime
which are never saved as files (cp.GenerateInMemory = true;).
The generated assemblies are hierachically dependent on each other so
I generate the "bottom" assemblies first.
How do I add a dependency to another previously loaded (generated)
assembly?
I would be happy if CompilerParameters.ReferencedAssemblies.Add could
take a System.Reflection.Assembly reference as parameter.
A possible solution would be to generate dll files and reference them
but I like the idea of not having any files to cleanup when my
application exits.
因为你没有将它们保存到磁盘,为什么不建立一个
程序集?
FB
-
------------------ -------------------------------------------------- ----
LLBLGen Pro的首席开发人员,.NET的高效O / R映射器
LLBLGen Pro网站: http://www.llblgen.com
我的.NET博客: http://weblogs.asp.net/fbouma
Microsoft MVP(C#)
-------------------------- ----------------------------------------------
Because you''re not saving them to disk, why not build a single
assembly?
FB
--
------------------------------------------------------------------------
Lead developer of LLBLGen Pro, the productive O/R mapper for .NET
LLBLGen Pro website: http://www.llblgen.com
My .NET blog: http://weblogs.asp.net/fbouma
Microsoft MVP (C#)
------------------------------------------------------------------------
好点。我之前一直在使用该解决方案但是因为我没有提到
应用程序/系统可以构建更多的程序集,同时执行一些已经构建的
。
Frans Bouma [C#MVP]"写道:
Good point. I have earlier been using that solution but as I didn''t mention
the application/system can build more assemblies while executing some already
builded ones.
"Frans Bouma [C# MVP]" wrote:
Jan写道:
Jan wrote:
我正在使用CSharpCodeProvider来构建一些永远不会保存的程序集@ runtime
as files(cp.GenerateInMemory = true;)。
生成的程序集在层次上彼此依赖,因此我生成了bottom。首先是程序集。
如何向另一个先前加载(生成)的程序集添加依赖项?
如果CompilerParameters.ReferencedAssemblies.Add可以<将System.Reflection.Assembly引用作为参数。
一个可能的解决方案是生成dll文件并引用它们
但我喜欢没有任何文件要清理的想法当我的
应用程序退出时。
I''m using the CSharpCodeProvider to buils some assemblies @ runtime
which are never saved as files (cp.GenerateInMemory = true;).
The generated assemblies are hierachically dependent on each other so
I generate the "bottom" assemblies first.
How do I add a dependency to another previously loaded (generated)
assembly?
I would be happy if CompilerParameters.ReferencedAssemblies.Add could
take a System.Reflection.Assembly reference as parameter.
A possible solution would be to generate dll files and reference them
but I like the idea of not having any files to cleanup when my
application exits.
因为你没有将它们保存到磁盘,为什么不构建一个
程序集?
> FB
-
---------------------------------- --------------------------------------
LLBLGen Pro的首席开发人员,富有成效适用于.NET的O / R映射器
LLBLGen Pro网站: http://www.llblgen.com
我的.NET博客: http://weblogs.asp.net / fbouma
Microsoft MVP(C#)
------------------------------------------------- -----------------------
Because you''re not saving them to disk, why not build a single
assembly?
FB
--
------------------------------------------------------------------------
Lead developer of LLBLGen Pro, the productive O/R mapper for .NET
LLBLGen Pro website: http://www.llblgen.com
My .NET blog: http://weblogs.asp.net/fbouma
Microsoft MVP (C#)
------------------------------------------------------------------------
我相信这可以满足您的一切需求原谅邋code的代码...我今天晚些时候会发布一篇关于它的博客文章,并在这里发布链接。
使用系统;
使用System.CodeDom.Compiler;
使用Microsoft.CSharp;
使用System.Reflection;
命名空间ConsoleApplication9
{
///< summary>
/// Class1的摘要说明。
///< ; / summary>
class Class1
{
class Program {
static void GenerateMainAssembly(){
Microsoft.CSharp.CSharpCodeProvider codeProvider = new
Microsoft.CSharp.CSharpCodeProvider();
ICodeCompiler compiler = codeProvider.CreateCompiler() ;
CompilerParameters parameters = new CompilerParameters(new string []
{" mscorlib.dl l"});
parameters.ReferencedAssemblies.Add(" System.dll");
parameters.ReferencedAssemblies.Add(" inmemoryassem bly。 dll);
parameters.GenerateExecutable = false;
parameters.GenerateInMemory = true;
string source ="" ;;
source + =" public class MyObject {" ;;
source + =" public void Test(){" ;;
source + =" System.Console.WriteLine(\" Test \");" ;;
source + =" OtherObject.SomeMethod();" ;;
source + =" System.Console.ReadLine();" ;;
source + =" }" ;;
source + ="}" ;;
CompilerResults results = compiler.CompileAssemblyFromSource(参数,
来源);
//调用测试
object o = results.CompiledAssembly.CreateInstance(" MyObject");
类型t = o .GetType();
MethodInfo mi = t.GetMethod(" Test");
mi.Invoke(o,null);
}
private static void CheckResults(CompilerResults Results){
if(Results.Errors.Count> 0){
抛出新的System.Exception(" Compile Failed");
}
}
static void GenerateOtherAssembly(){
CSharpCodeProvider codeProvider = new
Microsoft.CSharp.CSharpCodeProvider();
ICodeCompiler compiler = codeProvider.CreateCompiler ();
CompilerParameters parameters = new CompilerParameters(new string []
{" mscorlib.dll"});
>
par ameters.ReferencedAssemblies.Add(" System.dll");
parameters.OutputAssembly =" inmemoryassembly.dll" ;;
parameters.GenerateExecutable = false ;
parameters.GenerateInMemory = true;
string source ="" ;;
source + =" public class OtherObject {" ;;
source + =" public static void SomeMethod(){" ;;
source + =" System.Console.WriteLine(\" foo \");" ;;
source + =" }" ;;
source + ="}" ;;
CompilerResults results = compiler.CompileAssemblyFromSource(参数,
来源);
CheckResults(结果);
}
static void Main(string [] args){
GenerateOtherAssembly();
GenerateMainAssembly();
}
}
}
}
" Jan" < J0 **************** @ world.dk>在消息中写道
news:22 ********************************** @ microsof t.com ...
I believe this does everything you need it to excuse the sloppy code ... I
will put up a blog post later today about it and post link here.
using System;
using System.CodeDom.Compiler;
using Microsoft.CSharp;
using System.Reflection;
namespace ConsoleApplication9
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
class Program {
static void GenerateMainAssembly() {
Microsoft.CSharp.CSharpCodeProvider codeProvider = new
Microsoft.CSharp.CSharpCodeProvider();
ICodeCompiler compiler = codeProvider.CreateCompiler();
CompilerParameters parameters = new CompilerParameters(new string[]
{"mscorlib.dll"});
parameters.ReferencedAssemblies.Add("System.dll");
parameters.ReferencedAssemblies.Add("inmemoryassem bly.dll");
parameters.GenerateExecutable = false;
parameters.GenerateInMemory = true;
string source = "";
source += "public class MyObject {";
source += " public void Test() {";
source += " System.Console.WriteLine(\"Test\");";
source += " OtherObject.SomeMethod();";
source += " System.Console.ReadLine();";
source += " }";
source += "}";
CompilerResults results = compiler.CompileAssemblyFromSource(parameters,
source);
//call test
object o = results.CompiledAssembly.CreateInstance("MyObject" );
Type t = o.GetType();
MethodInfo mi = t.GetMethod("Test");
mi.Invoke(o, null);
}
private static void CheckResults(CompilerResults Results) {
if(Results.Errors.Count > 0) {
throw new System.Exception("Compile Failed");
}
}
static void GenerateOtherAssembly() {
CSharpCodeProvider codeProvider = new
Microsoft.CSharp.CSharpCodeProvider();
ICodeCompiler compiler = codeProvider.CreateCompiler();
CompilerParameters parameters = new CompilerParameters(new string[]
{"mscorlib.dll"});
parameters.ReferencedAssemblies.Add("System.dll");
parameters.OutputAssembly = "inmemoryassembly.dll";
parameters.GenerateExecutable = false;
parameters.GenerateInMemory = true;
string source = "";
source += "public class OtherObject {";
source += " public static void SomeMethod() {";
source += " System.Console.WriteLine(\"foo\");";
source += " }";
source += "}";
CompilerResults results = compiler.CompileAssemblyFromSource(parameters,
source);
CheckResults(results);
}
static void Main(string[] args) {
GenerateOtherAssembly();
GenerateMainAssembly();
}
}
}
}
"Jan" <j0****************@world.dk> wrote in message
news:22**********************************@microsof t.com...
我正在使用CSharpCodeProvider构建一些程序集@ runtime,它们从未保存为文件(cp.GenerateInMemory = true;)。
生成的程序集在层次上彼此依赖,因此我生成底部。首先是程序集。
如何将依赖项添加到另一个先前加载(生成)的程序集中?
如果CompilerParameters.ReferencedAssemblies.Add可以采用,我会很高兴
一个
System.Reflection.Assembly引用作为参数。
一个可能的解决方案是生成dll文件并引用它们但是
我喜欢我的申请退出时没有任何文件清理的想法。
任何建议表示赞赏。
/ Jan
I''m using the CSharpCodeProvider to buils some assemblies @ runtime which
are
never saved as files (cp.GenerateInMemory = true;).
The generated assemblies are hierachically dependent on each other so I
generate the "bottom" assemblies first.
How do I add a dependency to another previously loaded (generated)
assembly?
I would be happy if CompilerParameters.ReferencedAssemblies.Add could take
a
System.Reflection.Assembly reference as parameter.
A possible solution would be to generate dll files and reference them but
I
like the idea of not having any files to cleanup when my application
exits.
Any suggestions appreciated.
/Jan
>
这篇关于CSharpCodeProvider:引用其他生成的“InMemory” assembl的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!