{"id":5122,"date":"2019-09-30T22:40:46","date_gmt":"2019-09-30T14:40:46","guid":{"rendered":"http:\/\/blog.qdac.cc\/?p=5122"},"modified":"2019-10-02T09:51:05","modified_gmt":"2019-10-02T01:51:05","slug":"%e6%95%99%e7%a8%8b%e5%9f%ba%e4%ba%8e-rtti-%e5%88%9b%e5%bb%ba%e7%89%b9%e5%ae%9a%e7%b1%bb%e5%9e%8b%e7%9a%84%e5%ae%9e%e4%be%8b","status":"publish","type":"post","link":"https:\/\/blog.qdac.cc\/?p=5122","title":{"rendered":"[\u6559\u7a0b]\u57fa\u4e8e RTTI \u521b\u5efa\u7279\u5b9a\u7c7b\u578b\u7684\u5b9e\u4f8b"},"content":{"rendered":"\n<p>\u5728 Delphi \u4eba\u7fa4\u4e2d\uff0c\u6709\u4e9b\u4eba\u603b\u60f3\u901a\u8fc7\u540d\u79f0\uff0c\u7136\u540e\u52a8\u6001\u521b\u5efa\u6307\u5b9a\u7c7b\u7684\u5b9e\u4f8b\u800c\u4e0d\u5f97\u5176\u95e8\u800c\u5165\uff0c\u4e0d\u5f97\u4e0d\u5c06\u7c7b\u578b\u5148\u6ce8\u518c\uff0c\u7136\u540e\u518d\u56de\u8c03\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u3002\u5b9e\u9645\u4e0a\uff0c\u901a\u8fc7 RTTI \u6765\u521b\u5efa\u5b9e\u4f8b\u662f\u6709\u529e\u6cd5\u7684\u3002\u4e0b\u9762\u662f\u57fa\u672c\u7684\u6b65\u9aa4\uff1a<\/p>\n\n\n\n<p>1\u3001\u627e\u5230\u7c7b\u578b\u5bf9\u5e94\u7684 TRttiType \u4fe1\u606f\u3002\u8fd9\u91cc\u6709\u51e0\u4e2a\u65b9\u6cd5\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>GetType \u65b9\u6cd5\uff0c\u8fd9\u4e2a\u9700\u8981\u77e5\u9053\u76ee\u6807\u7c7b\u578b\u7684 TClass\/PTypeInfo \u4fe1\u606f\u3002<\/li><li>FindType \u65b9\u6cd5\uff0c\u8fd9\u4e2a\u9700\u8981\u77e5\u9053\u76ee\u6807\u7c7b\u578b\u7684\u5b8c\u6574\u540d\u79f0\uff0c\u6211\u77e5\u9053\u8fd9\u5c31\u662f\u8bb8\u591a\u4eba\u60f3\u8981\u7684\uff0c\u901a\u8fc7\u5b83\u53ef\u4ee5\u627e\u5230\u5bf9\u5e94\u7684 TRttiType \u4fe1\u606f\u3002<\/li><li>\u81ea\u5df1\u904d\u5386\u6240\u6709\u7684\u7c7b\u578b\uff0c\u627e\u5230\u7b26\u5408\u81ea\u5df1\u8981\u6c42\u7684\u76ee\u6807\u7c7b\u3002<br>\u4e0b\u9762\u662f\u53c2\u8003\u7684\u4ee3\u7801\uff1a<\/li><\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>function FindType(const AName:String):TRttiType;\nvar\n   I:Integer;\n   ATypes:TArray&lt;TRttiType>;\nbegin\n  Result:=nil;\n  ATypes:=TRttiContext.Create.GetTypes();\n  for I:=0 to High(ATypes) do\n  begin\n    \/\/\u6ce8\u610f\u8fd9\u79cd\u76f4\u63a5\u6bd4\u8f83\u540d\u79f0\u7684\u5c40\u9650\u6027\uff0c\u5982\u679c\u4e0d\u540c\u5355\u5143\u6709\u540c\u540d\u7684\u7c7b\u578b\uff0c\u53ef\u80fd\u4f1a\u5f97\u5230\u4f60\u4e0d\u60f3\u8981\u7684\u7ed3\u679c\uff0c\u66f4\u4fdd\u9669\u7684\u662f\u7528 QualifiedName\uff0c\u5b83\u5305\u542b\u4e86\u5355\u5143\u540d\uff0c\u6bd4\u5982 test.TTest\uff0c\u8fd9\u6837\u53ef\u4ee5\u907f\u514d\u5355\u5143\u95f4\u91cd\u540d\u7684\u95ee\u9898\uff0c\u4f46\u53c2\u6570\u4f20\u8fc7\u6765\u65f6\uff0c\u4e5f\u8981\u6ce8\u610f\u3002\u8fd9\u91cc\u5982\u679c\u4e0d\u8981\u533a\u5206\u5927\u5c0f\u5199\uff0c\u6362\u6210\n    \/\/if CompareText(ATypes[I].Name,AName)=0 then\n    \/\/\u5982\u679c\u8003\u8651\u7684\u66f4\u591a\u4e00\u70b9\uff0c\u5c06 Package.Name \u7684\u68c0\u67e5\u4e5f\u52a0\u4e0a\uff0c\u907f\u514d DLL \u548c\u4e3b\u7a0b\u5e8f\u91cc\uff0c\u5355\u5143\u540d\u91cd\u7684\u95ee\u9898\n    if ATypes[I].Name=AName then\n      Exit(ATypes[I]);\n  end;\nend;<\/code><\/pre>\n\n\n\n<p>2\u3001\u4e0d\u7ba1\u7528\u4ec0\u4e48\u529e\u6cd5\uff0c\u53cd\u6b63\u4f60\u5df2\u7ecf\u627e\u5230\u4e86\u76ee\u6807\u7c7b\u578b\u7684 TRttiType \u4fe1\u606f\uff0c\u90a3\u4e48\u73b0\u5728\uff0c\u4f60\u5c31\u9700\u8981\u505a\u7b2c\u4e8c\u6b65\uff1a\u60f3\u529e\u6cd5\u627e\u5230\u521b\u5efa\u5b9e\u4f8b\u7684\u65b9\u6cd5\u3002\u8fd9\u4e2a\u64cd\u4f5c\u5b9e\u4f8b\u4e0a\u5c31\u7275\u6d89\u5230\u51e0\u4e2a\u95ee\u9898\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>\u6211\u4eec\u662f\u5426\u5141\u8bb8\u521b\u5efa\u5305\u542b\u7eaf\u865a\u51fd\u6570\u7684\u5b9e\u4f8b\u3002Delphi \u672c\u8eab\u867d\u7136\u5141\u8bb8\u521b\u5efa\uff0c\u4f46\u5b9e\u9645\u4e0a\u5e76\u4e0d\u662f\u4ec0\u4e48\u597d\u4e60\u60ef\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7 TRttiType.GetMethods \u8fd4\u56de\u5bf9\u5e94\u7684\u65b9\u6cd5\u6570\u7ec4\uff0c\u68c0\u67e5\u5176\u4e2d\u662f\u5426\u5b58\u5728 Code=nil \u7684\u51fd\u6570\uff0c\u6765\u5224\u65ad\u5b83\u662f\u5426\u5305\u542b\u7eaf\u865a\u65b9\u6cd5\u3002<\/li><li>\u627e\u5230\u7b26\u5408\u6211\u4eec\u8981\u6c42\u7684\u6784\u9020\u51fd\u6570\u7248\u672c\u3002\u4e00\u822c\u6765\u8bf4\uff0c\u5982\u679c\u7528\u6237\u5c06\u6307\u5b9a\u7684\u6784\u9020\u51fd\u6570\u653e\u5230 protected \/private \u91cc\u65f6\uff0c\u8bf4\u660e\u4f5c\u8005\u4e0d\u5e0c\u671b\u6211\u4eec\u53bb\u8c03\u7528\u5b83\uff0c\u6240\u4ee5\u6211\u4eec\u53ea\u627e public \u91cc\u7684\u6784\u9020\u51fd\u6570\uff08\u5982\u679c\u4f60\u653e published \u91cc\u7b97\u6211\u8f93\uff0c\u53cd\u6b63\u6211\u627e\u4e0d\u5230\u653e\u5230 published \u91cc\u7684\u7406\u7531\uff09\u3002<br>\u9664\u975e\u4e8b\u5148\u7ea6\u5b9a\uff0c\u5426\u5219\u6211\u4eec\u65e0\u6cd5\u77e5\u9053\u5e26\u53c2\u6570\u7684\u6784\u9020\u51fd\u6570\u7684\u53c2\u6570\u8be5\u4f20\u5565\u503c\u3002\u6240\u4ee5\uff0c\u6211\u4eec\u8981\u521b\u5efa\u5b9e\u4f8b\u65f6\uff0c\u8be5\u5b9e\u4f8b\u5fc5\u9700\u662f\u6211\u4eec\u80fd\u591f\u660e\u786e\u638c\u63a7\u7684\u3002\u540e\u9762\u7684\u4f8b\u5b50\uff0c\u6211\u4eec\u4f7f\u7528\u7684\u662f\u65e0\u53c2\u6570\u6570\u7684\u6784\u9020\u51fd\u6570\u7248\u672c\u3002\u5982\u679c\u8981\u5e26\u53c2\u6570\u7684\u7248\u672c\uff0c\u6bd4\u5982\u521b\u5efa\u4e00\u4e2a TComponent \u7684\u5b50\u7c7b\u5b9e\u4f8b\uff0c\u90a3\u4f60\u5b8c\u5168\u53ef\u4ee5\u5c06\u5176\u552f\u4e00\u7684\u53c2\u6570\u4f20\u9012\u4e3a Application \u4e00\u7c7b\u7684\u503c\u3002\u5177\u4f53\u5927\u5bb6\u81ea\u5df1\u7814\u7a76\u3002<\/li><\/ul>\n\n\n\n<p>3\u3001\u6839\u636e\u524d\u4e24\u6b65\u7684\u4fe1\u606f\uff0c\u6211\u4eec\u521b\u5efa\u51fa\u65b0\u7684\u5b9e\u4f8b\u3002\u53c2\u8003\u4ee3\u7801\u5982\u4e0b<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>procedure TForm1.Button1Click(Sender:TObject)\nvar\n  AObj:TSimpleObject;\n  AType:TRttiType;\n  AMethod,AConstructor:TRttiMethod;\n  AMethods:TArray&lt;TRttiMethod>;\nbegin\n   \/\/\u627e\u5230\u7c7b\u578b\uff0c\u8fd9\u91cc\u7b80\u5316\uff0c\u76f4\u63a5\u4f7f\u7528\u5df2\u77e5\u7684\u7c7b\u578b\n   AType:=TRttiContext.Create.GetType(TypeInfo(TSimpleObject));  \n   \/\/\u627e\u5230\u8981\u6c42\u7684\u6784\u9020\u51fd\u6570\u5e76\u68c0\u67e5\u6ca1\u6709\u7eaf\u865a\u51fd\u6570\n   AConstructor := nil;\n   AMethods := AType.GetMethods;\n   for I := 0 to High(AMethods) do\n      begin\n      if AMethods[I].IsConstructor then\n         begin\n         if (AMethods[I].Visibility = mvPublic) and\n            (Length(AMethods[I].GetParameters) = 0) then\n            AConstructor := AMethods[I];\n           end\n        else if AMethods[I].CodeAddress = nil then\n          raise EAbstractError.CreateFmt(SCreateAbstractClass,\n            [AType.Name]);\n      end;\n   if not Assigned(AMethods) then\n      raise EConvertError.CreateFmt(SNoDefaultConstructor,[AType.Name]);\n   \/\/\u521b\u5efa\u5bf9\u5e94\u7c7b\u578b\u7684\u5b9e\u4f8b\uff0c\u6ce8\u610f\u6b64\u65f6\u6ca1\u6709\u8c03\u7528\u6784\u9020\u51fd\u6570\uff0c\u53ea\u662f\u5206\u914d\u4e86\u5185\u5bb9\u5e76\u521d\u59cb\u5316\u4e86\u7c7b\u578b\u4fe1\u606f\n   AObj := AType.Handle.TypeData.ClassType.NewInstance;\n   try\n      \/\/ \u8c03\u7528\u9ed8\u8ba4\u6784\u9020\u51fd\u6570\u521b\u5efa\u5b9e\u4f8b\n      AConstructor.Invoke(AObj, []);\n   Except\n      on E: Exception do\n         begin\n         FreeAndNil(AObj);\n         raise;\n         end;\n   end;\nend;<\/code><\/pre>\n\n\n\n<p>\u597d\u4e86\uff0c\u672c\u6559\u7a0b\u5230\u6b64\u7ed3\u679c\uff0c\u611f\u8c22\u60a8\u5230\u6b64\u4e00\u6e38\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 Delphi \u4eba\u7fa4\u4e2d\uff0c\u6709\u4e9b\u4eba\u603b\u60f3\u901a\u8fc7 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[69,8,603],"tags":[172,296,775],"class_list":["post-5122","post","type-post","status-publish","format-standard","hentry","category-c-builder","category-delphi","category-603","tag-rtti","tag-296","tag-775"],"views":3809,"_links":{"self":[{"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=\/wp\/v2\/posts\/5122","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5122"}],"version-history":[{"count":3,"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=\/wp\/v2\/posts\/5122\/revisions"}],"predecessor-version":[{"id":5126,"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=\/wp\/v2\/posts\/5122\/revisions\/5126"}],"wp:attachment":[{"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5122"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5122"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5122"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}