{"id":5820,"date":"2014-04-07T09:29:32","date_gmt":"2014-04-07T09:29:32","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/04\/07\/reflection-for-objects-declared-in-a-type-collection-of-common-programming-errors\/"},"modified":"2014-04-07T09:29:32","modified_gmt":"2014-04-07T09:29:32","slug":"reflection-for-objects-declared-in-a-type-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/04\/07\/reflection-for-objects-declared-in-a-type-collection-of-common-programming-errors\/","title":{"rendered":"Reflection for objects declared in a type-Collection of common programming errors"},"content":{"rendered":"<p>I have two classes, a parent and a child class.<\/p>\n<p>public class Parent<\/p>\n<p>{<br \/>\npublic Parent()<\/p>\n<p>{<\/p>\n<p>}<\/p>\n<p>public object getVariableValue(object instance, string variableName)<\/p>\n<p>{<\/p>\n<p>return instance.GetType().GetField(variableName).GetValue(instance);<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p><\/p>\n<p>public<\/p>\n<p>class Employees<\/p>\n<p>{<\/p>\n<p>public string name;public int salary;public Employees()<\/p>\n<p>{<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>public class Child : Parent<\/p>\n<p>{<\/p>\n<p>public Employees emp1;public int x;public Child()<\/p>\n<p>{<\/p>\n<p>emp1 =<\/p>\n<p>new Employees();<\/p>\n<p>emp1.name = &#8220;jhon&#8221;;<\/p>\n<p>emp1.salary = 1200;<\/p>\n<p>x = 2323;<\/p>\n<p>}<\/p>\n<p>}<br \/>\nwhen I try to get the value of a field using reflection, it works successfully<\/p>\n<p>\nChild cc = new Child();object o = cc.getVariableValue(cc,&#8221;x&#8221;);<\/p>\n<p>now I need to get the value of the object &#8220;emp&#8221; members (name and salary). is there a way to do that?<\/p>\n<p>m gado<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I have two classes, a parent and a child class. public class Parent { public Parent() { } public object getVariableValue(object instance, string variableName) { return instance.GetType().GetField(variableName).GetValue(instance); } } public class Employees { public string name;public int salary;public Employees() { } } public class Child : Parent { public Employees emp1;public int x;public Child() { [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-5820","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/5820","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=5820"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/5820\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=5820"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=5820"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=5820"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}