2

Let's say I have:

@{
var str= "DateTime.Now";
}

I want to process this string as a c# code

@Html.Raw(App.ProcessAsCode(str));

The output should be the current date time.

eadam
  • 23,151
  • 18
  • 48
  • 71
  • 2
    Why would you need anything like this? – Paweł Bejger Feb 06 '14 at 23:41
  • 3
    "Best" would likely be to "don't do that" - this is asking how to "`eval`". Basically, unless you *need* to accept arbitrary code as text (which is potentially dangerous and generally only useful for things like online "fiddles"), *don't*. So .. what's the *real* problem being solved? xD – user2864740 Feb 06 '14 at 23:42
  • `run a string as c# code` Do you mean what the compiler does ? You **shouldn't** do something like this and I don't see any reason to do that. – Selman Genç Feb 06 '14 at 23:43
  • 1
    @Selman22 Arbitrary code *can* be run, given the appropriate environment and context. Consider how a tool like LINQPad, or the Immediate Window in the debugger. – user2864740 Feb 06 '14 at 23:43
  • @user2864740 ofcourse it is possible if you think like that,what I mean by you can't is it's highly unnecessary in this case. – Selman Genç Feb 06 '14 at 23:45
  • 2
    @Selman22: You're confusing the words "can't" and "probably shouldn't". – David Feb 06 '14 at 23:46
  • @David okey, I change it,do you happy now :) – Selman Genç Feb 06 '14 at 23:47
  • Often when I use a CMS text editor to write html, I think of writing simple code like #DateTime.Now# in the html editor and the page will process it as c# code NOT html. This is just a question that keep coming into my mind sometimes. – eadam Feb 06 '14 at 23:52
  • @Ibn-e-Adam If you are using this for a CMS based in .NET I would recommend looking into the .Liquid engine. See: https://github.com/formosatek/dotliquid/ – drew_w Feb 06 '14 at 23:56
  • @drew_w thank you for your answer. I will have a look – eadam Feb 06 '14 at 23:59

2 Answers2

5

Final Edit:

Based on further information - if the goal here is to simply have a formatting engine there are lots of options out there. One such option is based around the .liquid syntax from shopify (see here). You can find a .NET port of this on gitHub here: https://github.com/formosatek/dotliquid/. The main purpose of this is to turn something like:

 <h2>{{product.name}}</h2>

Into something like:

 <h2>Beef Jerky</h2>

I would strongly recommend reading more about the liquid engine and syntax and I believe this will lead you in the right direction. Best of luck!


Initial Answer

This is definitely possible - although as others have said you will want to be careful in what you do. Using C# the key to compiling and running code generically is the "CSharpCodeProvider" class. Here is a brief example of how that looks:

        string[] references = { "System.dll" };
        CompilerParams.ReferencedAssemblies.AddRange(references);

        var provider = new CSharpCodeProvider();
        CompilerResults compile = provider.CompileAssemblyFromSource(CompilerParams, formattedCode);

In this example, "formattedCode" is a string with the C# code. Any references must be manually added. For the full example see this stack question (How to get a Type from a C# type name string?).

NOTE -- If all you are looking to do here is a format string or something simple like that you might have the user pass in a .NET format string (eg "MM/dd/yyyy"), then use that in a call to the "ToString" method. That would provide the user some configurability, while still making sure your system stays secure. In general running code on a server that hasn't been properly checked/escaped is really dangerous!

Reference - For your reference, the current msdn page for CSharpCodeProvider also has some examples.

Community
  • 1
  • 1
drew_w
  • 10,320
  • 4
  • 28
  • 49
  • Thanks for your answer, please see my comment above. Just looking for the fastest way to write html PLUS simple c# snippets in a text editor. – eadam Feb 06 '14 at 23:55
  • @Ibn-e-Adam I believe my update (posted to the beginning of the question) is a good place to start. You are basically looking for a templating engine like .liquid. Best of luck! – drew_w Feb 07 '14 at 00:01
0

Another option would be using a dynamic language such as IronRuby or IronPython.

mason
  • 31,774
  • 10
  • 77
  • 121