1

I've been trying for hours on finding a way to save a dynamic Assembly (I have access to the assembly - it is of the type 'Assembly'). Also, I do not have access to the byte array of the assembly. I've also looked around StackOverflow, but this search did not yield successful results.

For clarification:

// the assembly (this is an example)
Assembly asm = SomeMethod(); // returns a valid result
// now how would I go about saving to disk?

Is it possible to save the an assembly of type Assembly?
If so, how would I proceed to accomplish this?

Thanks in advance.

For people commenting "possible duplicate": This is a duplicate, but with valid reason: the answers on other threads do not work - this is an attempt to revive a topic with low-quality answers.

This will not work because the answer does not instantiate "results" and therefore I cannot replicate the solution.

AssemblyBuilder.Save() will not work because supposedly C# "cannot cast type AssemblyBuilder to type Assembly", even though some of the other answers on here include this as a solution.

Community
  • 1
  • 1
Scott
  • 183
  • 12
  • If you believe it is duplicate correct way of asking for new answers is to offer bounty. If you think this is new variant - make sure to provide link in your question and explain why it did not work for you. – Alexei Levenkov Feb 06 '16 at 04:43
  • Alright, I will add bounty after it is eligible. – Scott Feb 06 '16 at 06:12
  • Based on question you've linked to ("save dynamically generated assembly") it does not look like it is duplicate as you trying to save assembly generated by someone else (or just loaded by someone else) - which is not directly supported (obviously you can collect all info via reflection and re-create similar assembly, but it would have different identity/not exactly the same content). – Alexei Levenkov Feb 06 '16 at 07:04
  • It was the first result on Google (for "C# save dynamic assembly"). – Scott Feb 06 '16 at 07:14
  • You may want to clarify for future visitors whether this question is indeed about saving dynamic assembly ([IsDynamic](https://msdn.microsoft.com/en-us/library/system.reflection.assembly.isdynamic%28v=vs.110%29.aspx) set to `true`) or you had some other meaning of "dynamic" in mind. – Alexei Levenkov Feb 06 '16 at 07:17
  • I mean dynamic as in "in-memory" and "no defined place" (loaded in memory). I also believe this to be what future visitors are expecting. – Scott Feb 06 '16 at 07:29

1 Answers1

2

did you try using Mono.Cecil project? It offers a variety of APIs to inject IL and/or edit code and also you can save your assembly with it. I myself used it in a project and AFAIK from your question, it could help you on your problem, too.

Vahid K.
  • 440
  • 4
  • 14
  • I'm trying to use Mono.Cecil - I see that AssemblyDefinition contains a Write(string) method, but I need to either convert the Assembly to AssemblyDefinition or get the bytes from the assembly. Do you know of any way to convert an Assembly to an AssemblyDefinition? – Scott Feb 06 '16 at 06:59
  • Really, I chose the hard way and reconstructed my assembly using ILs. – Vahid K. Feb 06 '16 at 07:06
  • That's what I'm doing right now - will comment again after I test the results. – Scott Feb 06 '16 at 07:12
  • THANK YOU :) I edited the IL to convert the bytes from Assembly to the write method :) – Scott Feb 06 '16 at 07:43