Saving Types generated via Reflection.Emit as code file (.cs) instead of saving it in .dll files

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

Answers


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.


Generally either the string classes or CodeDOM (http://msdn.microsoft.com/en-us/library/y2k85ax6.aspx) are used to generate C# source.


Need Your Help

How do I pass parameters using redirector helper in Zend Framework?

zend-framework

Is it possible to pass parameters ($_POST or $_GET) with redirectory helper in Zend Framework? The following code redirect to index action of current controller, but I would like to pass some param...

Find the sum of an infinite series

linux bash shell unix ubuntu

How can I find the sum of an infinite series?