Compile C # code dynamically without using the original required assemblies

I am compiling C # code written by end user dynamically in my software. In order to compile the code, I need to add some assemblies as a reference to CompilerParameters.

CompilerParameters loParameters = new CompilerParameters();
loParameters.ReferencedAssemblies.Add("C:\A.dll")
loParameters.ReferencedAssemblies.Add("C:\B.dll")

      

So I have to release these assemblies ("A.dll" and "B.dll") with my software, but for security reasons I don't want the user to be able to reverse engineer the source of these assemblies.

Note 1 . I can remove the implementation of the methods in the required assemblies (A and B), but my assemblies are very large and I cannot do it manually.

Note 2 . I know about obfuscation, but I don't want to use that.

Note 3 : I just need to compile the code successfully and I don't want to run it.

0


source to share


1 answer


I am assuming A.dll

and B.dll

are the assemblies, which are yours?

If so, then the approach would only be to expose the interfaces to those libraries (i.e., place your interfaces and implementations in separate assemblies) and then dispatch the interface assemblies.

This way there would be nothing to decompile / reverse engineer, and that would keep the compiler happy.



To generate these assemblies easily, you can use Roslyn to: remove any private (fields, properties, methods, etc.) or unnecessary (static constructors, built-in initializers, etc.), replace the collection of public methods of public classes throw new NotImplementedException () for example, and only compile that.

Assemblies will then be created that would be just wrappers, but would effectively display the correct interfaces, which would be sufficient for the compiler.

0


source







All Articles