0

I am using CSharpCompilation.Create to create assembly from scratch. The thing I got to know is this always creates a new assembly. Is there anyway to add/modify the existing DLL in Roslyn? This is because I am building a tool where a user can type C# code in richtextbox and compile it to add it to the existing assembly. Is it possible, if not what I am doing wrong and what is the right way?

P.S.,

I have also tried CSharpCodeProvider as well to compile the code, but it is also Generating assembly with only the code I typed in richtextbox. The output assembly did not have other classes in that.

8
  • 1
    What's the reason that the new code must be in the same assembly? If it is really required, you could compile to a new assembly and then merge using ILMerge. Or - whar's basically the same - disassemble (ildasm) the existing one to IL, add your code and assemble again (ilasm). Commented Feb 15, 2022 at 14:28
  • Yes I need the new code to be part of same assembly. Is there any way to do this how Visual Studio does? If we add a class and compile in VS, it will be added to same assembly. I need something similar. And everything should be done programmatically. Commented Feb 15, 2022 at 14:42
  • Visual Studio has all the source code and will compile everything anew. Commented Feb 15, 2022 at 14:43
  • Can we do it programmatically in similar way? Commented Feb 15, 2022 at 14:45
  • Read my comment again: VS does not take one input assembly and add newly compiled code to it (what you want to do). VS will compile everything and output it to a new assembly (which requires the complete source code). Commented Feb 15, 2022 at 14:48

1 Answer 1

1

Yes as mentioned in comment by Klauss Gutter, you have to build the assembly individually and merge it later using ILMerge. I know ILMerge is out of support and is deprecated. You can use ILRepack to do this. It provides the same functionality as ILMerge.

You can use NuGet Package to install ILRepack first and then add reference to the ILRepack.exe to your project to use its methods to pack multiple assemblies in to one.

Roslyn or CSharpProvider wont help you to merge your assemblies. The main motto of these are to compile and create an assembly not merge.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, it is working.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.