Created
February 7, 2011 16:19
-
-
Save keithbloom/814638 to your computer and use it in GitHub Desktop.
Using anonymous types to set test data.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[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