{"id":3954,"date":"2016-06-14T14:36:28","date_gmt":"2016-06-14T06:36:28","guid":{"rendered":"http:\/\/blog.qdac.cc\/?p=3954"},"modified":"2016-06-14T14:36:28","modified_gmt":"2016-06-14T06:36:28","slug":"%e7%bf%bb%e8%af%91-attribute-%e5%92%8c-rtti","status":"publish","type":"post","link":"https:\/\/blog.qdac.cc\/?p=3954","title":{"rendered":"[\u7ffb\u8bd1] Attribute \u548c RTTI"},"content":{"rendered":"<p><strong>Attribute <\/strong><strong>\u548c RTTI \u00a0(Berlin\u5e2e\u52a9\u7ffb\u8bd1\u6574\u7406)<\/strong><\/p>\n<p><span style=\"color: #ff0000\">\u6ce8\uff1a\u57fa\u4e8e\u672c\u4eba\u82f1\u6587\u6c34\u5e73\uff0c\u4ee5\u4e0b\u7ffb\u8bd1\u53ea\u662f\u6211\u81ea\u5df1\u7684\u7406\u89e3\uff0c\u5982\u5bf9\u8bfb\u8005\u9020\u6210\u672a\u77e5\u5f71\u54cd\uff0c\u4e00\u5207\u540e\u679c\u81ea\u8d1f\u3002<\/span><\/p>\n<p>\u4ecb\u7ecdattribute\u7684\u57fa\u672c\u6982\u5ff5\uff0c\u4e00\u822c\u4f7f\u7528\u60c5\u51b5\uff0c\u548c\u4e00\u4e9b\u5728Delphi\u4e2d\u7684\u9650\u5236\u3002<\/p>\n<p>\u6ce8\uff1aDelphi attribute\u5728C++Builder\u4e2d\u4e0d\u652f\u6301\u3002 Attribute\u4e00\u822c\u7ffb\u8bd1\u4e3a\u201c\u5c5e\u6027\u201d\uff0c\u8fd9\u91cc\u4fdd\u7559\u539f\u6837\u3002<\/p>\n<p><strong>\u4ecb\u7ecd<\/strong><\/p>\n<p>Attribute \u662fDelphi\u8bed\u8a00\u7684\u7279\u6027,\u5141\u8bb8\u4f60\u6ce8\u91ca(\u6807\u6ce8)\u7c7b\u578b\u548c\u7c7b\u578b\u6210\u5458\u4e00\u4e9b\u7279\u5b9a\u7684\u989d\u5916\u4fe1\u606f. \u8fd9\u4e2a\u4fe1\u606f\u53ef\u4ee5\u5728\u8fd0\u884c\u65f6\u67e5\u8be2.<\/p>\n<p><strong>Attribute <\/strong><strong>\u548c RTTI <\/strong><\/p>\n<p>Attribute \u4e0d\u4f1a\u4fee\u6539\u7c7b\u578b\u548c\u7c7b\u578b\u6210\u5458\u7684\u884c\u4e3a. \u80fd\u591f\u5728\u7f16\u8bd1\u7684\u4e8c\u8fdb\u5236\u6587\u4ef6\u4e2d\u9644\u52a0Attribute, \u9996\u5148\u4f60\u9700\u8981\u6709RTTI\u4fe1\u606f,\u610f\u601d\u662f\u4f60\u4e0d\u80fd\u660e\u786e\u7684\u7981\u7528RTTI\u4fe1\u606f. \u6bd4\u5982,\u4e0b\u9762\u7684\u4ee3\u7801, <em>SomeCustomAttribute<\/em> \u5c06\u4e0d\u4f1a\u7f16\u8bd1\u5230\u4e8c\u8fdb\u5236\u6587\u4ef6\u4e2d, \u56e0\u4e3a\u5728\u7c7b<em>TderivedObject<\/em> \u4e0a\u660e\u786e\u7981\u7528\u4e86RTTI \u4fe1\u606f.<\/p>\n<p>Type<\/p>\n<p>\/\/\u7f16\u8bd1\u6307\u4ee4\uff0c\u7981\u7528\u7c7b\u7684\u6240\u6709RTTI\u4fe1\u606f<\/p>\n<p>{$RTTI EXPLICIT METHODS([]) PROPERTIES([]) FIELDS([])}<\/p>\n<p>TDerivedObject = class(TObject)<\/p>\n<p>[SomeCustomAttribute()]\u00a0\u00a0 \/\/\u8fd9\u4e2a\u4e0d\u4f1a\u7f16\u8bd1\u5230\u4e8c\u8fdb\u5236\u6587\u4ef6\u4e2d<\/p>\n<p>procedure Shoot;<\/p>\n<p>end;<\/p>\n<p><strong>\u00a0<\/strong><\/p>\n<p><strong>\u58f0\u660eAttribute (RTTI)<\/strong><\/p>\n<p>\u4ecb\u7ecd\u521b\u5efaattributes\u7684\u57fa\u672c\u65b9\u6cd5, attribute \u7c7b\u7684\u9002\u5f53\u8bbe\u8ba1, \u548c\u4e00\u822c\u4f7f\u7528\u60c5\u51b5.<\/p>\n<p><strong>\u58f0\u660e\u4e00\u4e2aAttribute <\/strong><\/p>\n<p>\u4e00\u4e2aattribute \u662f\u4e00\u4e2a\u7b80\u5355\u7684class type. \u5b9a\u4e49\u81ea\u5df1\u7684attribute, \u4f60\u5fc5\u987b\u4ece\u7c7b: System.TCustomAttribute\u4e2d\u7ee7\u627f:<\/p>\n<p>type<\/p>\n<p>MyCustomAttribute = class(TCustomAttribute)<\/p>\n<p>end;<\/p>\n<p>\u73b0\u5728MyCustomAttribute \u53ef\u4ee5\u6807\u6ce8\u4efb\u4f55\u7c7b\u578b\u548c\u7c7b\u578b\u6210\u5458(\u6bd4\u5982class, record, \u6216 interface):<\/p>\n<p>type<\/p>\n<p>[MyCustomAttribute]\u00a0\u00a0 \/\/\u6807\u6ce8\u7c7b\u578b<\/p>\n<p>TSpecialInteger = type integer;<\/p>\n<p>TSomeClass = class<\/p>\n<p>[MyCustomAttribute]\u00a0\u00a0 \/\/\u6807\u6ce8\u7c7b\u7684\u6210\u5458<\/p>\n<p>procedure Work;<\/p>\n<p>end;<\/p>\n<p>\u8bf7\u6ce8\u610f\uff0c\u58f0\u660e\u7684attribute\u7c7b\u4e0d\u80fd\u88ab\u58f0\u660e\u4e3a\u62bd\u8c61\u7c7b\uff0c\u4e0d\u5e94\u8be5\u5305\u542b\u4efb\u4f55\u62bd\u8c61\u65b9\u6cd5. \u5373\u4f7f\u53ef\u4ee5\u7f16\u8bd1\u901a\u8fc7, \u4f46\u7f16\u8bd1\u751f\u6210\u7684\u4e8c\u8fdb\u5236\u6587\u4ef6\u4e0d\u5305\u542battribute\u4fe1\u606f.<\/p>\n<p><strong>Attribute<\/strong><strong>\u7684\u540d\u79f0\u4e2d\uff0c\u4ee5 \u201cAttribute\u201d \u7ed3\u675f\u7684\u88ab\u9690\u5f0f\u7f29\u77ed<\/strong><\/p>\n<p>\u5047\u5982\u4f60\u58f0\u660e\u4e862\u4e2a\u76f8\u540c\u524d\u7f00\u7684 TCustomAttribute \u7684\u5b50\u7c7b, \u6709\u4e00\u4e2a\u6709&#8217;Attribute&#8217; \u540e\u7f00, \u50cf\u8fd9\u6837:<\/p>\n<ul>\n<li>MyCustom<\/li>\n<li>MyCustomAttribute<\/li>\n<\/ul>\n<p>\u6709\u540e\u7f00 &#8216;Attribute&#8217; \u7684\u7c7b (MyCustomAttribute) \u4e00\u76f4\u53ef\u7528, \u77ed\u540d\u79f0\u7684\u7c7b (MyCustom) \u53d8\u6210\u4e0d\u53ef\u7528(\u56e0\u4e3a\u524d\u4e00\u4e2a\u5df2\u7ecf\u88ab\u9690\u5f0f\u7684\u7f29\u77ed\u4e86\uff0c\u9020\u6210\u540c\u540d).<\/p>\n<p>\u4ee5\u4e0b\u4ee3\u7801\u6f14\u793a\u8fd9\u4e2a\u95ee\u9898.<\/p>\n<pre class=\"lang:delphi decode:true\">type\r\n\r\n\u00a0 \/\/\u6d4b\u8bd5\u6a21\u68f1\u4e24\u53ef\u7684\u540d\u5b57\r\n\u00a0 TestAttribute = class(TCustomAttribute)\r\n\u00a0 end;\r\n\r\n\u00a0 \/\/ \u53d8\u6210\u4e0d\u53ef\u8bbf\u95ee\r\n\u00a0 Test = class(TCustomAttribute)\r\n\u00a0 end;\r\n\r\n\u00a0 [Test] \/\/ \u8fd0\u884c\u65f6\u662fTestAttribute\r\n\u00a0 TAmbigiousClass = class\r\n\u00a0 end;<\/pre>\n<p><strong>Attribute<\/strong><strong>\u4e2d\u7684\u6784\u9020\u65b9\u6cd5 <\/strong><\/p>\n<p>\u901a\u5e38,\u4e00\u4e2aattribute \u88ab\u8bbe\u8ba1\u6210\u643a\u5e26\u4e00\u4e9b\u9644\u52a0\u7684\u4fe1\u606f\u7528\u4e8e\u8fd0\u884c\u65f6\u67e5\u8be2. Attribute\u7c7b\u5141\u8bb8\u6307\u5b9a\u4e00\u4e9b\u81ea\u5b9a\u4e49\u7684\u4fe1\u606f, \u53ef\u4ee5\u5b9a\u4e49\u6784\u9020\u65b9\u6cd5constructor :<\/p>\n<p>type<\/p>\n<p>AttributeWithConstructor = class(TCustomAttribute)<\/p>\n<p>public<\/p>\n<p>constructor Create(const ASomeText: String);<\/p>\n<p>end;<\/p>\n<p>\u7136\u540e\u53ef\u4ee5\u8fd9\u6837\u4f7f\u7528:<\/p>\n<p>type<\/p>\n<p>[AttributeWithConstructor(&#8216;Added text value!&#8217;)]<\/p>\n<p>TRecord = record<\/p>\n<p>FField: Integer;<\/p>\n<p>end;<\/p>\n<p><strong>\u6ce8\u91ca(\u6807\u6ce8) \u7c7b\u578b\u548c\u7c7b\u578b\u6210\u5458<\/strong><\/p>\n<p>\u8fd9\u4e2a\u4e3b\u9898\u63cf\u8ff0\u901a\u8fc7attribute\u6807\u6ce8\u7c7b\u578b\u548c\u7c7b\u578b\u6210\u5458\u7684\u8bed\u6cd5\u548c\u89c4\u5219.<\/p>\n<p><strong>\u4e00\u822c\u8bed\u6cd5 <\/strong><\/p>\n<p>\u6807\u6ce8\u4e00\u4e2a Delphi \u7c7b\u578b\u6216\u6210\u5458, \u4f8b\u5982\u4e00\u4e2aclass \u6216 class \u6210\u5458, \u5fc5\u987b\u653e\u5728\u7c7b\u578b\u6216\u7c7b\u578b\u6210\u5458\u7684\u58f0\u660e\u4e4b\u524d:<\/p>\n<p>[CustomAttribute]<\/p>\n<p>TMyClass = class;<\/p>\n<p>\u5982\u679cattribute \u7c7b\u7684\u540d\u5b57\u662f\u4ee5&#8221;Attribute&#8221;\u7ed3\u5c3e\u7684,\u4f60\u53ef\u4ee5\u7701\u7565 &#8220;Attribute&#8221; \u540e\u7f00:<\/p>\n<p>[Custom]<\/p>\n<p>procedure DoSomething;<\/p>\n<p>\u5728attributes\u7684\u540d\u5b57\u540e\u52a0\u4e00\u7ec4\u62ec\u53f7\u4e5f\u662f\u4e00\u4e2a\u6709\u6548\u7684\u8bed\u6cd5:<\/p>\n<p>[Custom()]<\/p>\n<p>TMyRecord = record;<\/p>\n<p>\u4e00\u4e9battribute \u63a5\u53d7\u53c2\u6570. \u8981\u4f20\u9012\u53c2\u6570, \u4f7f\u7528\u50cf\u8c03\u7528\u65b9\u6cd5\u4e00\u6837\u7684\u8bed\u6cd5:<\/p>\n<p>[Custom(Argument1, Argument2, \u2026)]<\/p>\n<p>TSimpleType = set of (stOne, stTwo, stThree);<\/p>\n<p>\u591a\u4e2aattribute \u6807\u6ce8\u4e00\u4e2a\u7c7b\u578b\u65f6, \u4f60\u53ef\u4ee5\u4f7f\u7528\u4e0a\u9762\u4efb\u4f55\u5176\u4e2d\u4e00\u79cd\u8bed\u6cd5:<\/p>\n<p>[Custom1]<\/p>\n<p>[Custom2(MyArgument)]<\/p>\n<p>FString: String;<\/p>\n<p>\u6216\u8005\u4f7f\u7528\u9017\u53f7\u5206\u9694:<\/p>\n<p>[Custom1, Custom2(MyArgument)]<\/p>\n<p>function IsReady: Boolean;<\/p>\n<p><strong>Attribute <\/strong><strong>\u7684\u53c2\u6570\u53ea\u80fd\u662f\u5e38\u91cf\u8868\u8fbe\u5f0f <\/strong><\/p>\n<p>\u4e00\u4e2aattribute \u6807\u6ce8\u7c7b\u578b\u6216\u6210\u5458\u65f6\uff0c\u88ab\u6807\u6ce8\u7684\u7c7b\u578b\u6216\u6210\u5458\u7684RTTI\u4fe1\u606f\u4e2d\u4f1a\u5305\u542battribute\u76f8\u5173\u7684\u4fe1\u606f\uff0c\u8fd9\u4e9b\u4fe1\u606f\u5305\u542b:<\/p>\n<ul>\n<li>Attribute \u7684\u7c7b\u578b.<\/li>\n<li>Constructor\u65b9\u6cd5\u7684\u6307\u9488.<\/li>\n<li>\u80fd\u8fc7\u6784\u9020\u65b9\u6cd5\u4f20\u9012\u7684\u5e38\u91cf\u5217\u8868.<\/li>\n<\/ul>\n<p>\u901a\u8fc7constructor\u65b9\u6cd5\u4f20\u9012\u7684\u53c2\u6570\u5fc5\u987b\u662f\u5e38\u91cf\u8868\u8fbe\u5f0f.\u56e0\u4e3a\u8fd9\u4e9b\u503c\u4f1a\u7f16\u8bd1\u5230\u4e8c\u8fdb\u5236\u6587\u4ef6:<\/p>\n<ul>\n<li>\u4f60\u53ef\u4ee5\u4f7f\u7528 constant expressions, \u5305\u62ec sets, strings, \u548c <u>ordinal expressions<\/u>.<\/li>\n<li>\u53ef\u4ee5\u4f7f\u7528 TypeInfo() \u4f20\u9012\u7c7b\u578b\u4fe1\u606f\uff0c\u56e0\u4e3a RTTI \u5757\u7684\u5730\u5740\u7f16\u8bd1\u65f6\u662f\u5df2\u77e5\u7684.<\/li>\n<li>\u53ef\u4ee5\u4f7f\u7528 <u>class references<\/u> \u56e0\u4e3a\u7c7b\u7684\u5f15\u7528\u5730\u5740\u7f16\u8bd1\u65f6\u662f\u5df2\u77e5\u7684.<\/li>\n<li>\u4e0d\u80fd\u4f7f\u7528 out \u6216 var \u53c2\u6570\uff0c\u56e0\u4e3a\u4ed6\u4eec\u8fd0\u884c\u65f6\u9700\u8981\u8ba1\u7b97\u53c2\u6570\u5730\u5740.<\/li>\n<li>\u4e0d\u80fd\u4f7f\u7528 <u>Addr()<\/u> \u6216 <u>@<\/u>.<\/li>\n<\/ul>\n<p>\u4e0b\u9762\u7684\u4ee3\u7801\u6f14\u793a\u4e86\u4e0d\u5408\u6cd5\u7684Attribute\u6807\u6ce8\u4f7f\u7528\u65b9\u6cd5:<\/p>\n<p>var<\/p>\n<p>a, b: Integer;<\/p>\n<p>type<\/p>\n<p>[SomeAttribute(a + b)]<\/p>\n<p>TSomeType = record<\/p>\n<p>\/\/ \u2026<\/p>\n<p>end;<\/p>\n<p>\u4ee5\u4e0b\u4ee3\u7801\u662f\u6b63\u786e\u7684:<\/p>\n<p>const<\/p>\n<p>a = 10;<\/p>\n<p>b = 20;<\/p>\n<p>type<\/p>\n<p>[SomeAttribute(a + b)]<\/p>\n<p>TSomeType = record<\/p>\n<p>\/\/ \u2026<\/p>\n<p>end;<\/p>\n<p>\u7f16\u8bd1\u65f6a\u548cb\u7684\u503c\u90fd\u662f\u660e\u786e\u7684; \u56e0\u6b64, \u53ef\u4ee5\u76f4\u63a5\u8ba1\u7b97\u8fd9\u4e2a\u8868\u8fbe\u5f0f.<\/p>\n<h1>\u5728\u8fd0\u884c\u65f6\u83b7\u53d6 Attributes<\/h1>\n<p>\u63d0\u4f9b\u600e\u6837\u83b7\u53d6attributes\u7684\u4fe1\u606f.<\/p>\n<h2>Attribute \u5b9e\u4f8b<\/h2>\n<p>\u67e5\u8be2\u65f6\u624d\u4f1a\u4ea7\u751f\u771f\u5b9e\u7684Attribute\u5b9e\u4f8b. \u610f\u601d\u662fAttribute\u5b9e\u4f8b\u4e0d\u4f1a\u81ea\u52a8\u521b\u5efa.<\/p>\n<p>\u53c2\u8003\u4ee5\u4e0b attribute \u5b9a\u4e49:<\/p>\n<pre class=\"lang:delphi decode:true\">type\u00a0\r\nTSpecialAttribute = class(TCustomAttribute)\u00a0\r\npublic\u00a0\u00a0\u00a0\r\nFValue: String;\u00a0\u00a0\u00a0\r\nconstructor Create(const AValue: String);\u00a0\r\nend;\r\n\r\nconstructor TSpecialAttribute.Create(const AValue: String);\r\nbegin\u00a0\r\nFValue := AValue;\r\nend;<\/pre>\n<p><em>TSpecialAttribute<\/em> \u7528\u6765\u6807\u6ce8:<\/p>\n<p>type\u00a0 [TSpecialAttribute(&#8216;Hello World!&#8217;)]<\/p>\n<p>TSomeType = record<\/p>\n<p>&#8230;<\/p>\n<p>end;<\/p>\n<p>\u4ece<strong>TSomeType<\/strong> \u7c7b\u578b\u83b7\u53d6attribute, \u5fc5\u987b\u901a\u8fc7 System.Rtti \u5355\u5143\u7684\u76f8\u5173\u51fd\u6570. \u4e0b\u9762\u662f\u793a\u4f8b\u4ee3\u7801:<\/p>\n<pre class=\"lang:delphi decode:true\">var\u00a0 LContext: TRttiContext;\u00a0\r\nLType: TRttiType;\u00a0\r\nLAttr: TCustomAttribute;\r\nbegin\u00a0\r\n{ Create a new Rtti context }\u00a0\r\nLContext := TRttiContext.Create\u00a0 \u00a0\r\n\r\n{ \u4eceTSomeType \u7c7b\u578b\u4e2d\u62bd\u53d6attribute\u4fe1\u606f }\u00a0\r\nLType := LContext.GetType(TypeInfo(TSomeType));\u00a0\u00a0\r\n\r\n{ Search for the custom attribute and do some custom processing }\u00a0\r\nfor LAttr in LType.GetAttributes() do\u00a0\u00a0\u00a0\r\nif LAttr is TSpecialAttribute then\u00a0\u00a0\u00a0\u00a0\u00a0\r\nWriteln(TSpecialAttribute(LAttr).FValue); \r\n\u00a0\u00a0\r\n{ Destroy the context }\u00a0\r\nLContext.Free;\r\n\r\nend;<\/pre>\n<p>\u5982\u4e0a\u9762\u4ee3\u7801\u6240\u793a, \u7528\u6237\u5fc5\u987b\u5199\u76f8\u5173\u4ee3\u7801. \u771f\u5b9e\u7684 attribute\u5b9e\u4f8b\u88ab\u521b\u5efa\u5728TRttiType.GetAttributes \u65b9\u6cd5. \u6ce8\u610f\u4f8b\u5b50\u4e2d\u6ca1\u6709\u91ca\u653eattribute\u5b9e\u4f8b; \u00a0TRttiContext \u4f1a\u91ca\u653e\u6240\u6709\u8d44\u6e90.<\/p>\n<h2>\u5f02\u5e38<\/h2>\n<p>\u56e0\u4e3aattribute\u7684\u5b9e\u4f8b\u662f\u80fd\u8fc7\u7528\u6237\u5199\u4ee3\u7801\u521b\u5efa\u7684, \u8fd9\u5c31\u6709\u53ef\u80fd\u5728\u521b\u5efa\u65f6\u4ea7\u751f\u5f02\u5e38. \u4e00\u822c\u63a8\u8350\u4f7f\u7528 <strong>try<\/strong> .. <strong>except<\/strong> \u8bed\u53e5\u5757\u6765\u67e5\u8be2 attribute.<\/p>\n<p>\u4e00\u4e2a\u5178\u578b\u7684 attribute \u6784\u9020\u65b9\u6cd5\u50cf\u4ee5\u4e0b\u8fd9\u6837:<\/p>\n<pre class=\"lang:delphi decode:true\">constructor TSpecialAttribute.Create(const AValue: String);\r\nbegin\u00a0 \r\nif AValue = '' then\u00a0\u00a0\u00a0 \r\nraise EArgumentException.Create('Expected a non-null string');\u00a0\u00a0 \r\nFValue := AValue;\r\nend;\r\n\u6807\u6ce8TSomeType \u65f6\u4f20\u9012\u4e86\u7a7a\u503c: \r\ntype\u00a0 \r\n[TSpecialAttribute('')]\u00a0 \r\nTSomeType = record\u00a0\r\n ...\u00a0 \r\nend;<\/pre>\n<p>\u5728\u8fd9\u4e2a\u60c5\u51b5\u4e0b, \u5c06\u4ea7\u751f\u5f02\u5e38 <strong>EArgumentException<\/strong> \uff0c\u63a8\u8350\u7528\u4ee5\u4e0b\u65b9\u6cd5:<\/p>\n<p>&nbsp;<\/p>\n<pre class=\"lang:delphi decode:true\">{ Search for the custom attribute and do some custom processing }\u00a0\r\ntry\u00a0\u00a0\u00a0\r\nfor LAttr in LType.GetAttributes() do \u00a0 \u00a0\r\nif LAttr is TSpecialAttribute then\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\r\nWriteln(TSpecialAttribute(LAttr).FValue);\u00a0\r\nexcept\u00a0\u00a0\u00a0\r\n{ ... Do something here ... }\u00a0\r\nend;<\/pre>\n<p><strong>\u7f16\u8bd1\u5668\u63d0\u4f9b\u7684 Attribute<\/strong><\/p>\n<p><u>Delphi compilers<\/u>\u5305\u542b\u4e86\u4e00\u4e9b\u7279\u5b9a\u7684Attribute<\/p>\n<p><strong>Ref <\/strong><\/p>\n<p>Ref attribute \u7528\u4e8e\u4fee\u9970\u51fd\u6570\u4e2d\u7684const\u53c2\u6570\uff0c\u8ba9\u53c2\u6570\u53d8\u6210\u5f15\u7528\u4f20\u9012 (\u4e0d\u662f\u503c\u4f20\u9012)\uff0c\u6bd4\u5982\uff1a<\/p>\n<p>function CompareStr(const S1, S2: string): Integer;<\/p>\n<p>function FunctionName(const [Ref] parameter1: Class1Name; [Ref] const parameter2: Class2Name);<\/p>\n<p><strong>Unsafe <\/strong><\/p>\n<p>\u5c06\u4e00\u4e2a\u51fd\u6570\u7684\u7ed3\u679c\u6807\u8bb0\u4e3a\u4e0d\u5b89\u5168\u7684\uff0c\u4f7f\u7f16\u8bd1\u5668\u5c06\u51fd\u6570\u7ed3\u679c\u89c6\u4e3a\u201cUnsafe\u201d\uff0c\u8fd9\u5c06\u7981\u7528\u5bf9\u8c61\u7684ARC\u7ba1\u7406(ARC management)<\/p>\n<p>\u6ce8\uff1aARC &#8211;&gt; Automatic Reference Counting \u5f15\u7528\u8bb0\u6570<\/p>\n<p>\u5728\u51fd\u6570\u4e0a\u4f7f\u7528 <strong>unsafe<\/strong> attribute ,\u4f7f\u7528\u4ee5\u4e0b\u8bed\u6cd5:<\/p>\n<p>[Result: Unsafe] function ReturnUnsafe: TObject;<\/p>\n<p>&nbsp;<\/p>\n<p><strong>\u8b66\u544a:<\/strong> <strong>[Unsafe]<\/strong> \u8fd8\u53ef\u4ee5\u5e94\u7528\u5230variables (members) \u548c parameters. \u5728System\u5355\u5143\u4ee5\u5916\u4f7f\u7528\uff0c\u662f\u5728\u975e\u5e38\u7f55\u89c1\u7684\u60c5\u51b5\u4e0b\u7684. \u5b83\u88ab\u8ba4\u4e3a\u662f\u5371\u9669\u7684\uff0c\u5b83\u7684\u4f7f\u7528\u662f\u4e0d\u5efa\u8bae\u7684\uff0c\u56e0\u4e3a\u6ca1\u6709\u4e0e\u5f15\u7528\u8ba1\u6570\u7684\u4ee3\u7801\u751f\u6210.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>\u6ce8\u610f:<\/strong> \u4f60\u53ea\u80fd\u901a\u8fc7\u4e00\u4e2a<strong>[Unsafe]<\/strong>\u53d8\u91cf\u5230\u4e00\u4e2a\u88ab\u6807\u8bb0\u4e3a<strong>[Unsafe]<\/strong>\u7684<strong>var<\/strong>\u6216<strong> out<\/strong>\u53c2\u6570\u3002\u4f60\u4e0d\u80fd\u5bf9\u6807\u8bb0<strong>[Unsafe] <\/strong>\u7684<strong>var<\/strong> \u6216 <strong>out<\/strong>\u53c2\u6570\u4f20\u9012\u4e00\u4e2a\u5f3a\u5f15\u7528(\u610f\u601d\u662f\u5b9e\u53c2\uff0c\u5f62\u53c2\u90fd\u8981\u6807\u8bb0\u6210<strong>[Unsafe]<\/strong>)<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Volatile <\/strong><\/p>\n<p>volatile attribute \u88ab\u7528\u4e8e\u6807\u8bb0\u5b57\u6bb5, \u8be5\u5b57\u6bb5\u662f\u53d7\u4e0d\u540c\u7684\u7ebf\u7a0b\u66f4\u6539\u7684, \u56e0\u6b64\uff0c\u4ee3\u7801\u751f\u6210\u65f6\uff0c\u662f\u4e0d\u4f1a\u4f18\u5316\u5728\u5bc4\u5b58\u5668\u6216\u53e6\u4e00\u4e2a\u4e34\u65f6\u5b58\u50a8\u4f4d\u7f6e\u4e2d\u590d\u5236\u4ed6\u7684\u503c.<\/p>\n<p>\u4f60\u53ef\u4ee5\u4f7f\u7528 volatile attribute \u53bb\u6807\u8bb0\u4e0b\u5217\u58f0\u660e:<\/p>\n<ul>\n<li>Variables (global and local)<\/li>\n<li>Parameters<\/li>\n<li>record \u6216 class \u7684\u5b57\u6bb5<\/li>\n<\/ul>\n<p>\u4e0d\u80fd\u4f7f\u7528 volatile attribute \u53bb\u6807\u8bb0\u4e0b\u5217\u58f0\u660e:<\/p>\n<ul>\n<li>Type<\/li>\n<li>Procedures, Functions or Methods<\/li>\n<li>Expressions<\/li>\n<\/ul>\n<p>type<\/p>\n<p>TMyClass = class<\/p>\n<p>private<\/p>\n<p>[volatile] FMyVariable: TMyType;\u00a0\u00a0 \/\/\u7c7b\u4e2d\u7684\u5bf9\u8c61\u7c7b\u578b\u4e5f\u4e0d\u53ef\u4ee5<\/p>\n<p>end;<\/p>\n<p><strong>Weak <\/strong><\/p>\n<p>weak attribute \u6807\u8bb0\u58f0\u660e\u4e3a\u5f31\u5f15\u7528(\u7528\u5728\u7c7b\u4e4b\u95f4\u7684\u76f8\u4e92\u5f15\u7528\u65f6\uff0c\u53ef\u53c2\u8003Berlin\u7248\u4e2d\u7684TownedCollection \u7c7b\u5b9a\u4e49)\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Attribute \u548c RTTI \u00a0(B [&hellip;]<\/p>\n","protected":false},"author":104,"featured_media":0,"comment_status":"open","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":[8],"tags":[],"class_list":["post-3954","post","type-post","status-publish","format-standard","hentry","category-delphi"],"views":3763,"_links":{"self":[{"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=\/wp\/v2\/posts\/3954","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\/104"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3954"}],"version-history":[{"count":4,"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=\/wp\/v2\/posts\/3954\/revisions"}],"predecessor-version":[{"id":3959,"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=\/wp\/v2\/posts\/3954\/revisions\/3959"}],"wp:attachment":[{"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3954"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3954"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3954"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}