将通过 Reflection.Emit 生成的类型保存为代码文件 (.cs),而不是将其保存在 .dll 文件中
在开始之前,让我先讲一下我的经验:我对 C#.NET、Web 服务、XML 部分等方面有丰富的经验。反射对我来说是新事物,尽管我已经广泛阅读了它并尝试了一些实验代码,但还没有使用
我检查过的反射做出任何伟大的事情 许多示例
说明我们如何在运行时创建类型,然后将其保存在程序集 (.dll) 文件中。我见过的所有示例都是关于将创建的类型保存在 .dll 文件而不是代码文件中。有没有办法通过反射创建代码文件?
我需要创建代码文件,因为我想分发代码而不是编译的程序集。我想要做的就像 xsd.exe 所做的那样,要么吐出 .dll 或代码文件(任何语言)。
有没有办法创建代码文件,因为我能找到的大部分地方都是
AssemblyBuilder ab = System.AppDomain.CurrentDomain.DefineDynamicAssembly(an, AssemblyBuilderAccess.Save);
最后
ab.Save("QuoteOfTheDay.dll");
Before start let me tell my experience: I am experienced with C#.NET, web services, XML part and few more. Reflection is something new to me, though I have read extensively on it and tried out some experimental code, but haven't made anything great using reflection
I checked out many examples
of how we can create Type at runtime and then which can be saved in an assembly (.dll) files. Of all the examples I have seen is about saving the created types in the .dll files instead of code file. Isn't there any way to create the code file out of reflection?
I need to create code file since I want to distribute code instead of compiled assemblies. What I want to do is something like xsd.exe
does, either spit out a .dll or the code file(in any language).
Isn't there any way to create a code file, since most of the place I can find is
AssemblyBuilder ab = System.AppDomain.CurrentDomain.DefineDynamicAssembly(an, AssemblyBuilderAccess.Save);
and then lastly
ab.Save("QuoteOfTheDay.dll");
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
.NET 框架中没有内置反汇编程序。您可以使用 Reflector 将发出的 IL 转换为其支持的源代码语言之一。这不太可能没有问题,Reflector 旨在将编译器生成的代码转换回源代码。与 System.Reflection.Emit 提供的随心所欲的方法不同,编译器往往对它们生成的 IL 持保守态度。
如果您想要源代码,那么最好首先生成它。使用 System.CodeDom。这就是 System.Xml.Serialization 所使用的。
There is no disassembler built into the .NET framework. You could use Reflector to convert the IL you emitted to one of the source code languages it supports. It isn't terribly likely that this will work without problems, Reflector was designed to convert code that was generated by a compiler back to source code. Compilers tend to be conservative about the IL they generate, unlike the anything-goes approach that System.Reflection.Emit offers.
If you want source code then you're best off by generating it in the first place. Use System.CodeDom. That's what System.Xml.Serialization uses.
通常是字符串类或 CodeDOM (http://msdn.microsoft.com/ en-us/library/y2k85ax6.aspx) 用于生成 C# 源代码。
Generally either the string classes or CodeDOM (http://msdn.microsoft.com/en-us/library/y2k85ax6.aspx) are used to generate C# source.