{"id":2866,"date":"2019-09-20T00:47:49","date_gmt":"2019-09-19T15:47:49","guid":{"rendered":"http:\/\/okamurax.com\/?p=2866"},"modified":"2020-02-03T18:40:41","modified_gmt":"2020-02-03T09:40:41","slug":"c-%e3%83%aa%e3%83%95%e3%83%ac%e3%82%af%e3%82%b7%e3%83%a7%e3%83%b3","status":"publish","type":"post","link":"https:\/\/appbay.org\/?p=2866","title":{"rendered":"C# Reflection"},"content":{"rendered":"<pre class=\"lang:c# decode:true \">class Test\r\n{\r\n    public string UserName = \"hello\";\r\n    public int UserAge = 0;\r\n    public Test()\r\n    {\r\n        UserAge = 10;\r\n    }\r\n}\r\n\r\npublic Form1()\r\n{\r\n    InitializeComponent();\r\n\r\n    \/\/ \u901a\u5e38\u30a2\u30af\u30bb\u30b9\r\n    Debug.Print(new Test().UserName); \/\/ hello\r\n\r\n    \/\/ \u30ea\u30d5\u30ec\u30af\u30b7\u30e7\u30f3\r\n\r\n    var testObj = new Test(); \/\/ \u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u751f\u6210\r\n    var objType = testObj.GetType(); \/\/ Type\u53d6\u5f97\r\n\r\n    Debug.Print(objType.Name); \/\/ Test\r\n\r\n    var fieldInfo = objType.GetField(\"UserAge\"); \/\/ Type\u304b\u3089FieldInfo\u53d6\u5f97 (\u5b58\u5728\u3057\u306a\u3044\u3068null)\r\n\r\n    Debug.Print(fieldInfo.GetValue(testObj).ToString()); \/\/ 10\r\n    \/\/ GetValue\u306f\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u3092\u6e21\u3059\u70b9\u306b\u6ce8\u610f\r\n\r\n    fieldInfo.SetValue(testObj, 20);\r\n    Debug.Print(testObj.UserAge.ToString()); \/\/ 20\r\n}<\/pre>\n<p>Getter\u306b\u30a2\u30af\u30bb\u30b9\u3059\u308b\u5834\u5408\u3082\u307b\u307c\u540c\u3058\u3002<\/p>\n<pre class=\"lang:c# decode:true \">foreach (var model in SalesDataGridViewModels)\r\n{\r\n  var objType = model.GetType();\r\n  var propInfo = objType.GetProperty(ColumnSelect.Text);\r\n  string targetString = propInfo.GetValue(model).ToString().Replace(\"\u3000\", \" \");\r\n}<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>class Test { public string UserName = &#8220;hello&#8221;; public int UserAge = 0; public Test() { UserAge = 10; } } publi &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/appbay.org\/?p=2866\" class=\"more-link\"><span class=\"screen-reader-text\">&#8220;C# Reflection&#8221; \u306e<\/span>\u7d9a\u304d\u3092\u8aad\u3080<\/a><\/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":[9],"class_list":["post-2866","post","type-post","status-publish","format-standard","hentry","category-1","tag-c-net"],"_links":{"self":[{"href":"https:\/\/appbay.org\/index.php?rest_route=\/wp\/v2\/posts\/2866","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/appbay.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/appbay.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/appbay.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/appbay.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2866"}],"version-history":[{"count":4,"href":"https:\/\/appbay.org\/index.php?rest_route=\/wp\/v2\/posts\/2866\/revisions"}],"predecessor-version":[{"id":5127,"href":"https:\/\/appbay.org\/index.php?rest_route=\/wp\/v2\/posts\/2866\/revisions\/5127"}],"wp:attachment":[{"href":"https:\/\/appbay.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2866"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/appbay.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2866"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/appbay.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2866"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}