Delphi Language Guide Delphi for Microsoft Win32 Delphi for the Microsoft. Net framework



Yüklə 0,84 Mb.
Pdf görüntüsü
səhifə291/294
tarix02.01.2022
ölçüsü0,84 Mb.
#41395
1   ...   286   287   288   289   290   291   292   293   294
DelphiLanguageGuide

Using Custom Attributes
Custom attributes are placed directly before the source code symbol to which the attribute applies. Attributes can
be placed before
245


variables and constants
procedures and functions
function results
procedure and function parameters
types
fields, properties, and methods
Note that Delphi for .NET supports the use of named properties in the initialization. These can be the names of
properties, or of public fields of the custom attribute class. Named properties are listed after all of the parameters
required by the constructor. For example
      [TCustomCodeAttribute(1024, prop1=512, prop2=128)]
      TMyClass = class(TObject)
      ...
      end;
applies the custom attribute declared above to the class 
TMyClass
.
The first parameter, 1024, is the value required by the constructor. The second two parameters are the properties
defined in the custom attribute.
When a custom attribute is placed before a list of multiple variable declarations, the attribute applies to all variables
declared in that list. For example
var
 [TCustomAttribute(1024, prop1=512, prop2=128)]
 x, y, z: Integer;
would result in 
TCustomAttribute
 being applied to all three variables, 
X

Y
, and 
Z
.
Custom attributes applied to types can be detected at runtime with the GetCustomAttributes method of the Type
class. The following Delphi code demonstrates how to query for custom attributes at runtime.
var
   F: TMyClass;          // TMyClass declared above
   T: System.Type;
   A: array of TObject;  // Will hold custom attributes
   I: Integer;
begin
   F := TMyClass.Create;
   T := F.GetType;
   A := T.GetCustomAttributes(True);
   // Write the type name, and then loop over custom 
   // attributes returned from the call to 
   // System.Type.GetCustomAttributes.
   Writeln(T.FullName);
   for I := Low(A) to High(A) do
      Writeln(A[I].GetType.FullName);
end.
246



Yüklə 0,84 Mb.

Dostları ilə paylaş:
1   ...   286   287   288   289   290   291   292   293   294




Verilənlər bazası müəlliflik hüququ ilə müdafiə olunur ©azkurs.org 2024
rəhbərliyinə müraciət

gir | qeydiyyatdan keç
    Ana səhifə


yükləyin