在内存中创建程序集
我想在内存中创建一个程序集,使用 Reflection.Emit
中的类,
目前,我可以创建程序集并使用类似的方法获取它的字节
AssemblyBuilder builder =
AppDomain.CurrentDomain.DefineDynamicAssembly(...,
AssemblyBuilderAccess.Save);
... create the assembly ...
builder.Save(targetFileName);
using(FileStream fs = File.Open(targetFileName, FileMode.Open))
{
... read the bytes from the file stream ...
}
但是,它是通过创建一个文件来实现的在本地文件系统上。我实际上不需要该文件,只需要文件中的字节。
是否可以在不写入任何文件的情况下创建程序集?
I'd like to create an assembly in memory, using an using the classes in Reflection.Emit
Currently, I can create the assembly and get it's bytes using something like
AssemblyBuilder builder =
AppDomain.CurrentDomain.DefineDynamicAssembly(...,
AssemblyBuilderAccess.Save);
... create the assembly ...
builder.Save(targetFileName);
using(FileStream fs = File.Open(targetFileName, FileMode.Open))
{
... read the bytes from the file stream ...
}
However, it does so by creating a file on the local filesystem. I don't actually need the file, just the bytes that would be in the file.
Is it possible to create the assembly without writing any files?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
目前这是不可能的。 C# 团队正在计划“编译器即服务”功能,希望将其包含在 C# 5.0 中。
This is not possible at this point in time. The C# team is planning a "compiler as a service" feature that hopefully will be included in C# 5.0.
调用
AssemblyBuilder.GetObjectData
来为您提供序列化的程序集数据,而不是查找字节怎么样?这可能已经足够好了。MSDN: http://msdn.microsoft.com/ en-us/library/system.reflection. assembly.getobjectdata.aspx
How about calling
AssemblyBuilder.GetObjectData
which gives you the serialized assembly data, rather than looking for the bytes? This is probably good enough.MSDN: http://msdn.microsoft.com/en-us/library/system.reflection.assembly.getobjectdata.aspx