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.

10
  • 2
    Why would you need anything like this? Commented Feb 6, 2014 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 Commented Feb 6, 2014 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. Commented Feb 6, 2014 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. Commented Feb 6, 2014 at 23:43
  • 2
    @Selman22: You're confusing the words "can't" and "probably shouldn't". Commented Feb 6, 2014 at 23:46

2 Answers 2

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.

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

2 Comments

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.
@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!
0

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

Comments

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.