将通过 Reflection.Emit 生成的类型保存为代码文件 (.cs),而不是将其保存在 .dll 文件中

发布于 2024-08-28 02:51:33 字数 654 浏览 3 评论 0原文

在开始之前,让我先讲一下我的经验:我对 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

无法言说的痛 2024-09-04 02:51:33

.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.

没有你我更好 2024-09-04 02:51:33

通常是字符串类或 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文