DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: How to translate string to Object

  1. #1
    Join Date
    Mar 2004

    How to translate string to Object

    Is there some sort of evaluation function in .Net that will let you take a string and evaluate it as an object?


    //Contact is a custom Object that contains properties for FirstName
    //And LastName

    Contact c = new Contact( "Alex Banks" );

    //This would write the value of the FirstName
    //Which in this case is Alex

    Response.Write( c.FirstName );

    //Now lets say we have a string that looks like this:
    string myObjString = "c.FirstName";

    //Is there a way to change this string into regular object code
    //So that we can use this string to access the value of c.FirstName?

    //For example in ActionScript you can say something like this:

    var theName = eval(myObjString);


    var theName = eval("c.FirstName');


    var theLastName = eval("c").LastName;

    //How can I go about doing something similar in C#.NET?

  2. #2
    Join Date
    Nov 2003
    Portland, OR
    I don't know of any way to evaluate the variable name ("c" in your example), but given an object instance, you can use reflection to access its fields, properties and methods:
    Phil Weber

    Please post questions to the forums, where others may benefit.
    I do not offer free assistance by e-mail. Thank you!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
Latest Articles
Questions? Contact us.
Web Development
Latest Tips
Open Source

   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center