The reflection Unit of Delphi 2009 (Objauto)

Source: Internet
Author: User
Tags reflection

The Objauto unit should be considered an extension of the Typinfo unit, right? It provides 5 methods:

GetMethods, Getmethodinfo, Createmethodpointer, Releasemethodpointer, Objectinvoke

The details of the public members of the class can be obtained through GetMethods, Getmethodinfo.

The information (example) of the members in the published area can only be obtained through typinfo;

Member information for the public area can also be obtained through Objauto.

This example effect chart:

This example has two units, and the Auxiliary Unit (UNIT2) holds 3 classes for testing:Unit Unit2;
Interface
Type
TCLASS1 does not specify additional compilation instructions, Objauto can only obtain methods of its published area
TClass1 = Class
function fun1:string;
Private
function fun1private:string;
Protected
function fun1protected:string;
Public
function fun1public:string;
Published
function fun1published:string;
End
TClass2 specified {$M +}, Objauto can get methods for its published and default regions.
In general, the default area's members are equivalent to the public area;
After {$M +} is specified, the member of the default zone is equivalent to the published area.
Since Tpersistent (which is the ancestor of many classes) has already specified {$M +}, most of the classes are {$M +} open.
{$M +}
TClass2 = Class
function fun2:string; {Default Zone}
Private
function fun2private:string;
Protected
function fun2protected:string;
Public
function fun2public:string;
Published
function fun2published:string;
End
{$M-}
The compiler directive Methodinfo is the Delphi 2009 new, only then it opens, Objauto can obtain the public area the information.
This allows Objauto to obtain information about the TCLASS3 's public, published, and default areas.
{$M +}
{$METHODINFO on}
TCLASS3 = Class
function fun3:string;
Private
function fun3private:string;
Protected
function fun3protected:string;
Public
function fun3public:string;
Published
function fun3published:string;
End
{$METHODINFO off}
{$M-}
Implementation
{TClass1-------------------------------------------------}
function TClass1.Fun1:string;
Begin
Result: = ' Fun1 ';
End
function TClass1.Fun1Private:string;
Begin
Result: = ' fun1private ';
End
function TClass1.Fun1Protected:string;
Begin
Result: = ' fun1protected ';
End
function TClass1.Fun1Public:string;
Begin
Result: = ' fun1public ';
End
function TClass1.Fun1Published:string;
Begin
Result: = ' fun1published ';
End
{TClass2-------------------------------------------------}
function TClass2.Fun2:string;
Begin
Result: = ' Fun2 ';
End
function TClass2.Fun2Private:string;
Begin
Result: = ' fun2private ';
End
function TClass2.Fun2Protected:string;
Begin
Result: = ' fun2protected ';
End
function TClass2.Fun2Public:string;
Begin
Result: = ' fun2public ';
End
function TClass2.Fun2Published:string;
Begin
Result: = ' fun2published ';
End
{TCLASS3-------------------------------------------------}
function TClass3.Fun3:string;
Begin
Result: = ' Fun3 ';
End
function TClass3.Fun3Private:string;
Begin
Result: = ' fun3private ';
End
function TClass3.Fun3Protected:string;
Begin
Result: = ' fun3protected ';
End
function TClass3.Fun3Public:string;
Begin
Result: = ' fun3public ';
End
function TClass3.Fun3Published:string;
Begin
Result: = ' fun3published ';
End
End.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.