{"id":3910,"date":"2014-03-30T06:20:51","date_gmt":"2014-03-30T06:20:51","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/create-function-to-loop-through-properties-of-a-method-in-c-collection-of-common-programming-errors\/"},"modified":"2014-03-30T06:20:51","modified_gmt":"2014-03-30T06:20:51","slug":"create-function-to-loop-through-properties-of-a-method-in-c-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/create-function-to-loop-through-properties-of-a-method-in-c-collection-of-common-programming-errors\/","title":{"rendered":"Create function to loop through properties of a method in C#?-Collection of common programming errors"},"content":{"rendered":"<p>How about something like:<\/p>\n<pre><code>void Main()\n{\nvar test = new TestObject();\ntest.a = \"123\";\ntest.b = \"456\";\nvar testProperties = (from prop in test.GetType().GetProperties() \n                        select new KeyValuePair(prop.Name,prop.GetValue(test,null).ToString()));\nforeach (var property in testProperties)\n{\n    Console.WriteLine(string.Format(\"Name: {1}{0}Value: {2}\",Environment.NewLine,property.Key,property.Value));\n}\n\nvar testMethods = (from meth in test.GetType().GetMethods() select new { Name = meth.Name, Parameters = \n    (from param in meth.GetParameters() \n        select new {Name = param.Name, ParamType=param.GetType().Name})} );\n\nforeach (var method in testMethods)\n{\n    Console.WriteLine(string.Format(\"Method: {0}\",method.Name));\n    foreach(var param in method.Parameters)\n    {\n        Console.WriteLine(\"Param: \" + param.Name + \" (\" + param.ParamType + \")\");\n    }\n}\n}\n\nclass TestObject\n{\npublic string a { get; set; }\npublic string b { get; set; }\npublic string testMethod(string param1,int param2){return string.Empty;}\n}\n<\/code><\/pre>\n<p>edit &#8211; Sorry I seem to have misread the question. I have no idea how to do what your asking, or even if it&#8217;s possible.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>How about something like: void Main() { var test = new TestObject(); test.a = &#8220;123&#8221;; test.b = &#8220;456&#8221;; var testProperties = (from prop in test.GetType().GetProperties() select new KeyValuePair(prop.Name,prop.GetValue(test,null).ToString())); foreach (var property in testProperties) { Console.WriteLine(string.Format(&#8220;Name: {1}{0}Value: {2}&#8221;,Environment.NewLine,property.Key,property.Value)); } var testMethods = (from meth in test.GetType().GetMethods() select new { Name = meth.Name, Parameters = (from param [&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-3910","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/3910","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=3910"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/3910\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=3910"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=3910"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=3910"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}