Skip to content

Instantly share code, notes, and snippets.

@keithbloom
Created February 7, 2011 16:19
Show Gist options
  • Save keithbloom/814638 to your computer and use it in GitHub Desktop.
Save keithbloom/814638 to your computer and use it in GitHub Desktop.
Using anonymous types to set test data.
[TestFixture, Explicit]
public class ReflectingAnonymousTypes
{
[Test]
public void DoesItWork()
{
StudentVisa student = Build.AStudentVisa();
var props = new {FirstName = "Keith", NationalityISO = "arj"};
SetProperties(student,props);
Assert.That(student.FirstName, Is.EqualTo(props.FirstName));
Assert.That(student.NationalityISO, Is.EqualTo(props.NationalityISO));
}
private void SetProperties(StudentVisa s, object parameters)
{
if (parameters == null) return;
var query = from v in s.GetType().GetProperties()
from p in parameters.GetType().GetProperties()
where p.Name == v.Name
select new {PropsToSet = v, Value = p.GetValue(parameters, null)};
foreach (var item in query)
{
item.PropsToSet.SetValue(s,item.Value, null);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment