{"id":478,"date":"2012-08-21T14:27:44","date_gmt":"2012-08-21T14:27:44","guid":{"rendered":"http:\/\/foxware.co.uk\/?p=478"},"modified":"2012-08-22T14:35:28","modified_gmt":"2012-08-22T14:35:28","slug":"dynamically-handling-lists-of-objects","status":"publish","type":"post","link":"http:\/\/foxware.co.uk\/?p=478","title":{"rendered":"Dynamically handling lists of objects"},"content":{"rendered":"<p>&nbsp;<\/p>\n<p>The following code shows how to create a list of different objects and handle there properties and methods. This example deals with a list of services.<\/p>\n<p>Create your object classes and add them to your list like this;<\/p>\n<pre class=\"lang:c# decode:true\">svcBusRelation = new clsBusRelation(); \r\n\r\nsvcBusRelationSector = new clsBusRelationSector();\r\n\r\nList&lt;object&gt; Services = new List&lt;object&gt;(); Services.Clear(); Services.Add(svcBusRelation); Services.Add(svcBusRelationSector);<\/pre>\n<p>This is a handy function for getting values of properties;<\/p>\n<pre class=\"lang:c# decode:true\">public static object GetPropValue(object src, string propName)\r\n{\r\nreturn src.GetType().GetProperty(propName).GetValue(src, null);\r\n}<\/pre>\n<p>To call a method from an object in the list;<\/p>\n<pre class=\"lang:c# decode:true \">private static void CallMethod(object src, string methodName)\r\n{\r\n        MethodInfo methodInfo = src.GetType().GetMethod(methodName);\r\n        methodInfo.Invoke(src, null);\r\n}<\/pre>\n<p>An example query;<\/p>\n<pre class=\"lang:c# decode:true\">var query = (from service in Services select new { Name = GetPropValue(service, \"Name\").ToString(), Status = GetPropValue(service, \"Status\").ToString(), IsRunning = (bool)GetPropValue(service, \"IsRunning\") }).ToList();<\/pre>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>&nbsp; The following code shows how to create a list of different objects and handle there properties and methods. This example deals with a list of services. Create your object classes and add them to your list like this; svcBusRelation &hellip; <a href=\"http:\/\/foxware.co.uk\/?p=478\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[5],"tags":[],"_links":{"self":[{"href":"http:\/\/foxware.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/478"}],"collection":[{"href":"http:\/\/foxware.co.uk\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/foxware.co.uk\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/foxware.co.uk\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/foxware.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=478"}],"version-history":[{"count":4,"href":"http:\/\/foxware.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/478\/revisions"}],"predecessor-version":[{"id":480,"href":"http:\/\/foxware.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/478\/revisions\/480"}],"wp:attachment":[{"href":"http:\/\/foxware.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=478"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/foxware.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=478"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/foxware.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=478"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}