0

I'm looking for a way to display code snippets stored in a database on a webpage with .NET and C# and I saw the exact function that I was looking for a while back, but I cannot remember what search terms I used or the name of the C# function that designed to do that specifically.

Essentially, it's a C# version of PHP's Eval() function, so I can render code examples that I've collected over the years and see the end result as well as the actual source code. I'm sure that this has already been asked, but I couldn't find what I was looking for when I looked through the existing questions on here.

This will be a password protected page on my site for my eyes only (theoretically, at least), so I'm not overly concerned about someone running malicious code. But I appreciate the kind warnings that some may feel obliged to offer before answering my question. Seriously, I do.

I have a feeling that about 10 people will see this question and go, "well, obviously it's (Insert Function Name Here), you silly wabbit." So this is my way of admitting my shame of not knowing right here, up front. Especially when I just saw it like a week ago.

Thanks in advance! Paul


Addendum: I found the following code and it works like a charm on HTML, JS & CSS. Since I've only been working with C# for a few months, I don't have a lot of C# code to render so it works quite well.

String EncodedString = rs["Code"].ToString();
StringWriter writer = new StringWriter();
Server.HtmlDecode(EncodedString, writer);
String DecodedString = writer.ToString();
codeDecoded.Text = DecodedString;
Realto619
  • 301
  • 3
  • 13
  • [LinqPad](http://www.linqpad.net/) may be enough (local tool) to serve you needs. – Alexei Levenkov May 08 '12 at 02:43
  • Check out http://stackoverflow.com/questions/10391866/running-external-c-sharp-file-in-a-program/10391948 and http://stackoverflow.com/questions/7721406/running-the-c-sharp-compiler-from-a-c-sharp-program/7721529#7721529 – The Mask May 08 '12 at 02:53
  • @ChrisGessler: Commenting on my question or the way I asked it? Hahaha... – Realto619 May 08 '12 at 08:44
  • @AlexeiLevenkov: I'm not looking for a tool, more like a C# function that would allow me to take code straight from a database and display it on a webpage so I can find pieces of code I've used on other projects and reuse it on a current one. I downloaded LinqPad though, and I plan on checking it out... – Realto619 May 08 '12 at 08:48
  • @Realto619 - in the past, it has not been easy writing 'Eval' methods for C# script. Microsoft has made it easier, but still a big subject. – Chris Gessler May 08 '12 at 11:37

2 Answers2

1

Take a look at the CSharpCodeProvider class.

A similar question was asked here.

Community
  • 1
  • 1
smqplus
  • 253
  • 1
  • 4
0

Just curious, are you looking for a REPL-like experience or just one-time evaluations?

Assuming the latter for now, if you can use Roslyn, their scripting API should work fine for this.

http://visualstudiomagazine.com/articles/2011/11/16/the-roslyn-scripting-api.aspx

James Manning
  • 13,429
  • 2
  • 40
  • 64
  • What I'm really looking for is a way to render HTML and C# code stored in a database on a web page. I have a lot of code examples that I've collected over the years that I would like to be able to display directly from stored records in a table, instead of just viewing the source code in a textarea field that I have to save as .html or .aspx.cs files and load elsewhere. I know that there's a C# function that does exactly what I want, but I can't remember it... – Realto619 May 08 '12 at 08:38
  • Well, for rendering HTML and C# code, I would normally recommend using the Razor engine (which you can use outside of asp.net fine). Is the C# code you want to run in the form of a full class definition, a method definition, or an expression (like "2+2")? – James Manning May 08 '12 at 13:11