1

Is there a way to populate the properties of an object from a string at runtime? Any library that can help? Exemplifying, I have this class:

public class TestObject
{
    public string Property1 { get; set; }
    public int Property2 { get; set; }
    public TestObject2 TestObject2 { get; set; }
}
public class TestObject2
{
    public string Property1 { get; set; }
}

In my real case, the class has several other sub-classes, arrays and others data types. I need to populate the properties from a text, file, list, with content like this:

TestObject.Property1 = "String Value"
TestObject.Property2 = 5
TestObject.TestObject2.Property1 = "Sub Property String"

One possibility I see is sweeping the text and assigning values​​/objects using Reflection. But before I'm looking for something already existing.

bodee
  • 2,654
  • 1
  • 16
  • 15
  • 4
    Have you considered using serialization/deserialization (XML or JSON)? – Rui Jarimba Nov 27 '13 at 16:50
  • Is your text file format fixed or are you allowed to decide how you want the values you want to use to populate the object? – p.s.w.g Nov 27 '13 at 16:50
  • Why don't you want to use serialization? However, if you decide to use Reflection, there is mine example in [this question](http://stackoverflow.com/questions/19936888/c-sharp-developing-net3-5-using-reflection-to-get-set-values-to-nested-properti/19938840#19938840) how it can be done - although code is for getters, setters will be very similar. – Konrad Kokosa Nov 27 '13 at 16:57
  • Each line of text represents a class property/sub-property, as described in example ("TestObject.Property2 = 5"), I receive it from another application, so I can not change the format. I created the classes according to the text format. The idea is to convert this text to objects, facilitating the use of this data within my system. – bodee Nov 27 '13 at 17:33

2 Answers2

0

I like the Newtonsoft Json library for this (http://james.newtonking.com/json). I'm sure there are many other libraries out there for this, but this is the one I have used and been happy with.

The output from your class would look something like:

        {
            "Property1":"Hello World",
            "Property2":42,
            "TestObject2":
            {
                "Property1":"Sub Hello World!"
            }
         }

The code to do this is simply Newtonsoft.Json.JsonConvert.SerializeObject(test) and the code to return the object is: TestObject test2 = Newtonsoft.Json.JsonConvert.DeserializeObject<TestObject>(jsonString);

JoelC
  • 3,664
  • 9
  • 33
  • 38
-1

Consider using Roslyn (although it is still CTP), where it can be easily done:

var testObject = new TestObject();
testObject.Property1 = "Value1";
testObject.Property2 = 44;
testObject.TestObject2 = new TestObject2();
testObject.TestObject2.Property1 = "NestedValue1";

var scriptEngine = new ScriptEngine();
scriptEngine.AddReference(typeof(TestObject).Assembly);
var session = scriptEngine.CreateSession(new HostObject() { TestObject = testObject });
session.Execute("TestObject.Property1 = \"Value2\"");
session.Execute("TestObject.TestObject2.Property1 = \"Sub Property String\"");

var test1 = testObject.Property1 == "Value2"; // true
var test2 = testObject.TestObject2.Property1 == "Sub Property String"; // true

with a little helper class:

public class HostObject
{
    public TestObject TestObject { get; set; }
}
Konrad Kokosa
  • 16,563
  • 2
  • 36
  • 58
  • Why downvote? It works, solves the problem and is cleaner that using Reflection. And @bodee can't change this format, so there is no way about it. – Konrad Kokosa Nov 28 '13 at 08:12