1

How can I compile raw C# code at runtime? So if I want to add security by removing important pieces of code and downloading it via a server, and then execute it from within my application?

JasonMArcher
  • 14,195
  • 22
  • 56
  • 52
Nahydrin
  • 13,197
  • 12
  • 59
  • 101

2 Answers2

0

I wrote this a while ago to simplify code execution, hope it helps:

CSharpCompiler

Large
  • 1
  • 1
0

Do you want to download the source code, or a compiled version?

You can download the MSIL and use Assembly.Load(byte[]) to load it for execution.

But none of these methods add security. If you send the code to the user's computer in ANY format, they can capture a copy. Never saving it to disk just adds a small speedbump.

See: The #1 law of software licensing.

Community
  • 1
  • 1
Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
  • The downloaded source code is encrypted. It's an online-only program, so the inconveneice will always be there. – Nahydrin Jul 05 '11 at 05:21
  • @Dark: Doesn't really matter, since the decryption keys are on the user's computer. Just another speedbump, no real security. – Ben Voigt Jul 05 '11 at 05:29
  • @Dark: Anyway, why send source code, when you can encrypt and send the MSIL output from the C# compiler? – Ben Voigt Jul 05 '11 at 05:41
  • Because I don't know how to do that. – Nahydrin Jul 05 '11 at 05:57
  • 1
    @Dark: Just build a DLL project. Send that file, when you decrypt it on the receiving side, instead of writing it to disk, call `Assembly.Load(byte[])`. – Ben Voigt Jul 05 '11 at 06:25