Project Description
Object transpose is a .NET 4.0 API for copying data between one ore more source objects and a destination object using the reflection API.
Sample Usage (version 1.0.5)
public class SingleStructure
{
private string m_value;
public string Value
{
get { return this.m_value; }
set { this.m_value = value; }
}
}
[Test]
public void SingleTest()
{
var source = new
{
Value = "test"
};
SingleStructure singleStructure = PropertyTranspose.ToValue<SingleStructure>(source);
// make sure the field was set
Assert.AreEqual(singleStructure.Value, source.Value);
}
Sample Usage (version 1.0.5)
public class ChildStructure
{
private string m_value;
private List<string> m_values;
public List<string> Values
{
get { return this.m_values; }
set { this.m_values = value; }
}
public string Value
{
get { return this.m_value; }
set { this.m_value = value; }
}
}
public class ParentStructure
{
private List<ChildStructure> m_childStructures;
public List<ChildStructure> ChildStructures
{
get { return this.m_childStructures; }
set { this.m_childStructures = value; }
}
}
[Test]
public void HierarchyTest()
{
List<string> values = new List<string>();
// add values
values.Add("apple");
values.Add("banana");
values.Add("grape");
values.Add("mango");
values.Add("mandarine");
values.Add("pear");
values.Add("peach");
// build the output
var groupedSources = values.GroupBy(value => value.Substring(0,1), (key, value) => new
{
Value = key,
Values = value
});
var sources = new
{
ChildStructures = groupedSources
};
ParentStructure parentStructure = PropertyTranspose.ToValue<ParentStructure>(sources);
// make sure the field was set
Assert.AreEqual(parentStructure.ChildStructures.Count, 5);
Assert.AreEqual(parentStructure.ChildStructures[0].Value, "a");
Assert.AreEqual(parentStructure.ChildStructures[0].Values.Count, 1);
Assert.AreEqual(parentStructure.ChildStructures[0].Values[0], "apple");
Assert.AreEqual(parentStructure.ChildStructures[1].Value, "b");
Assert.AreEqual(parentStructure.ChildStructures[1].Values.Count, 1);
Assert.AreEqual(parentStructure.ChildStructures[1].Values[0], "banana");
Assert.AreEqual(parentStructure.ChildStructures[2].Value, "g");
Assert.AreEqual(parentStructure.ChildStructures[2].Values.Count, 1);
Assert.AreEqual(parentStructure.ChildStructures[2].Values[0], "grape");
Assert.AreEqual(parentStructure.ChildStructures[3].Value, "m");
Assert.AreEqual(parentStructure.ChildStructures[3].Values.Count, 2);
Assert.AreEqual(parentStructure.ChildStructures[3].Values[0], "mango");
Assert.AreEqual(parentStructure.ChildStructures[3].Values[1], "mandarine");
Assert.AreEqual(parentStructure.ChildStructures[4].Value, "p");
Assert.AreEqual(parentStructure.ChildStructures[4].Values.Count, 2);
Assert.AreEqual(parentStructure.ChildStructures[4].Values[0], "pear");
Assert.AreEqual(parentStructure.ChildStructures[4].Values[1], "peach");
}