Eval a string in C#

How do I evaluate a string in C#?  Example:

string myVarName = "sTest";
eval("string "+myVarName+" = \"<p>Value of string goes here</p>\"");

Should output:
<p>Value of string goes here</p>
Who is Participating?

Improve company productivity with a Business Account.Sign Up

This is not really want you want, but might point you in the correct direction

using System;
using System.Reflection ;

namespace ConsoleApplication4
     /// <summary>
     /// Summary description for Class1.
     /// </summary>
     class Class1

          private class foo {
               private int bar;
               public int Bar {
                    get { return bar; }
                    set { bar = value ; }
               public foo() {}

          static object GetProperty(string Name, object Obj) {
               PropertyInfo p = Obj.GetType().GetProperty(Name) ;
               if(p!=null) {
                    if(p.CanRead) {
                         return p.GetValue(Obj, null) ;
                    } else {
                         throw new System.Exception(Name + " is a WriteOnly property cannot read value") ;
               } else {
                    throw new System.ArgumentOutOfRangeException(Name) ;

          static void Main(string[] args)
               foo f = new foo();
               f.Bar = 37;
               object o = GetProperty("Bar", f);
               Console.WriteLine("f.Bar = " + o.ToString() );
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

jjacksnConnect With a Mentor Commented:
After reading around, I don't think there is an equivelent to the JScript eval command.  

http://www.dotnet247.com/247reference/msgs/6/31515.aspx.  If you want to literally execute the strings, it looks like you will have to compile assemblies at run time.
theres a more recent thread (from yesterday in fact) http://www.experts-exchange.com/Programming/Programming_Languages/C_Sharp/Q_21088360.html

System.Web.UI.DataBinder.Eval() is what you want.
hendridmAuthor Commented:
Ok, thanks.  To be more specific, is there a way to do this?

string myVar = "Literal1";
Literal1.Text = "Lorem Ipsum";

Is there a way to programmatically specify 'Literal1', like so:

string myVar = "Literal1";
<name of variable in 'myVar'>.Text = "Lorem Ipsum";

If not, no problem.  You essentially answered my question anyway.
gregoryyoungConnect With a Mentor Commented:
for that you would be best off to use reflections ...
Bob LearnedCommented:
Are you talking about using Inline code with ASP.NET?

Bob LearnedCommented:
Something like this:

(1)  Define a page-level variable in public variable in the code-behind:

    Public var_value As Integer = 5

(2)  Use the HTML design to use the following inline code:


The page will display 5 within the block.

for the above you could also use FindControl() ...
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.