Note that there are some explanatory texts on larger screens.

plurals
  1. POC# and use of reflection
    text
    copied!<p>I came over this site (<a href="http://snipplr.com/view.php?codeview&amp;id=17637" rel="nofollow">http://snipplr.com/view.php?codeview&amp;id=17637</a>), which illustrates use of reflection like this: </p> <pre><code>public class Person { public int Age { get; set; } public string Name { get; set; } } private void button2_Click_1(object sender, EventArgs e) { var person = new Person { Age = 30, Name = "Tony Montana" }; var properties = typeof(Person).GetProperties(); foreach (PropertyInfo property in properties) { Console.WriteLine("{0} = {1}", property.Name, property.GetValue(person, null)); } } </code></pre> <p>The codesnippet above will give you: Age: 30 Name: Tony Montana</p> <p>What if we added "Kid" to the class "AnotherPerson" like this</p> <pre><code> public class Kid { public int KidAge { get; set; } public string KidName { get; set; } } public class AnotherPerson { public int Age { get; set; } public string Name { get; set; } public Kid Kid { get; set; } } </code></pre> <p>This snippet; </p> <pre><code>private void button3_Click(object sender, EventArgs e) { var anotherPerson = new AnotherPerson { Age = 30, Name = "Tony Montana", Kid = new Kid { KidAge = 10, KidName = "SomeName" } }; var properties = typeof(AnotherPerson).GetProperties(); foreach (PropertyInfo property in properties) { Console.WriteLine("{0} = {1}", property.Name, property.GetValue(anotherPerson, null)); } } </code></pre> <p>gives me: Age: 30 Name: Tony Montana Kid: ProjectName.Form1+Kid</p> <p>Not quite what I was looking for.... Could I use reflection to iterate trough "Kid" also? Suggestions?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload