2010-09-03 16 views
6

मुझे IInterface प्रकार के गुणों में कोई समस्या है। मुझे नहीं पता कि आरटीटीआईडेल्फी 2010 आरटीटीआई और इंटरफ़ेस फ़ील्ड्स

का उपयोग करके इन गुणों के मूल्यों को कैसे असाइन करना है, यहां एक उदाहरण दिया गया है:।

program Project2; 

uses 
    Forms, RTTI, Windows, TypInfo; 
{$R *.res} 

type 
    ITestInterfacedClass = interface 
    ['{25A5B554-667E-4FE4-B932-A5B8D9052A17}'] 
    function GetA: ITestInterfacedClass; 
    procedure SetA(const Value: ITestInterfacedClass); 
    property A: ITestInterfacedClass read GetA write SetA; 
    function GetB: ITestInterfacedClass; 
    procedure SetB(const Value: ITestInterfacedClass); 
    property B: ITestInterfacedClass read GetB write SetB; 
    end; 


    TTestInterfacedClass = class(TInterfacedObject, ITestInterfacedClass) 
    private 
    FA: ITestInterfacedClass; 
    FB: ITestInterfacedClass; 

    function GetA: ITestInterfacedClass; 
    function GetB: ITestInterfacedClass; 
    procedure SetA(const Value: ITestInterfacedClass); 
    procedure SetB(const Value: ITestInterfacedClass); 

    public 
    property A: ITestInterfacedClass read GetA write SetA; 
    property B: ITestInterfacedClass read GetB write SetB; 
    end; 


    { ITestInterfacedClass } 
.... 

procedure SetProperty(aLeft: TObject {IInterface}; aNameProp: string; aRight: IInterface); 
var 
    RttiContext: TRttiContext; 
    RttiType: TRttiType; 
    RTTIProperty: TRttiProperty; 
begin 
    RttiContext := TRttiContext.Create; 

    RTTIType := RttiContext.GetType(TTestInterfacedClass); 
    RTTIProperty := RTTIType.GetProperty(aNameProp); 
    if RTTIProperty.PropertyType.TypeKind = tkInterface then 
    RTTIProperty.SetValue(aLeft, TValue.From<IInterface>(aRight)); 
end; 

var 
    obj1: TTestInterfacedClass; 
    intf1, intf2, intf3: ITestInterfacedClass; 

begin 
    obj1 := TTestInterfacedClass.Create; 
    intf1 := obj1; 
    intf2 := TTestInterfacedClass.Create; 
    intf3 := TTestInterfacedClass.Create; 

    intf1.A := intf2; 

    // intf1.B := intf3; 
    SetProperty(obj1, 'B', intf3); 

end. 

मैं intf1.B की एक एनालॉग लिखने के लिए है: = intf3; या obj1.B = intf3;

आरटीटीआई का उपयोग कर।

क्या यह संभव है?

procedure SetProperty(aLeft: TObject; aNameProp: string; aRight: IInterface); 
var 
    RttiContext: TRttiContext; 
    RttiTypeInterface: TRttiInterfaceType; 
    RTTIProperty: TRttiProperty; 
    Value: TValue; 
begin 
    RttiContext := TRttiContext.Create; 

    RTTIType := RttiContext.GetType(aLeft.ClassType); 
    RTTIProperty := RTTIType.GetProperty(aNameProp); 
    if RTTIProperty.PropertyType.TypeKind = tkInterface then 
    begin 
    TValue.Make(@aRight, RTTIProperty.PropertyType.Handle, Value); 
    RTTIProperty.SetValue(aLeft, Value); 
    end; 
end; 
+0

आरटीटीआईप्रोपर्टी। सैटवैल्यू (एलीफ्ट, टीवील्यू। से (ITestInterfacedClass के रूप में aRight)); यह काम है, लेकिन SetProperty के अंदर मैं ITestInterfacedClass से कुछ भी नहीं चाहता हूं। – Mielofon

उत्तर

2

दुर्भाग्य से, वह काम नहीं करता क्योंकि RTTI.pas में इंटरफ़ेस रूपांतरण कोड QueryInterface कॉल नहीं करता है:

युपीडी यह काम है। यदि आप TValue.From<IInterface> के साथ एक टीवीएलू डालते हैं, तो आप इंटरफ़ेस को उस प्रकार का समर्थन करने के बावजूद इसे किसी भिन्न इंटरफ़ेस प्रकार के टीवीए में परिवर्तित नहीं कर सकते हैं। क्यूसी को जमा करने के लिए स्वतंत्र महसूस करें।

TValue.From<ITestInterfacedClass> के साथ टीवील्यू बनाना, हालांकि काम करता है। लेकिन फिर आप सरल SetProperty दिनचर्या का उपयोग नहीं कर सकते हैं।

+0

टीवील्यू.मेक ​​(@ARight, RTTIProperty.PropertyType.Handle, Value); आरटीटीप्रोपर्टी। सैटवैल्यू (एलीफ्ट, वैल्यू); – Mielofon

संबंधित मुद्दे