2016-11-14 33 views
4

我試圖發佈一個屬性,允許我在實現指定接口的所有組件之間進行選擇。 是否有可能做這樣的事情?使用接口作爲發佈的鏈接屬性

我嘗試使用一個接口作爲發佈的屬性,但它似乎並沒有工作。 下面是步驟,我已經按照:

我已經定義了兩個接口,併爲測試三個對象:

uses 
    Classes, Variants; 

type 
    IMyInterfaceA = interface 
    function FGetValue() : Variant; 
    end; 

    TMyObjectA = class(TComponent, IMyInterfaceA) 
    protected 
    FValue : Variant; 
    FAlternativeValueSource : IMyInterfaceA; 
    function FGetValue() : Variant; 
    published 
    property Value : Variant read FGetValue write FValue; 
    property AlternativeValueSource : IMyInterfaceA read FAlternativeValueSource write FAlternativeValueSource; 
    end; 

    IMyInterfaceB = interface 
    procedure DoSomething(); 
    end; 

    TMyObjectB = class(TComponent, IMyInterfaceB) 
    public 
    procedure DoSomething(); 
    end; 

    TMyObjectC = class(TComponent); 

implementation 

function TMyObjectA.FGetValue() : Variant; 
begin 
    if((FValue = Null) AND (FAlternativeValueSource <> nil)) 
    then Result := FAlternativeValueSource.FGetValue 
    else Result := FValue; 
end; 

procedure TMyObjectB.DoSomething(); 
begin 
    //do something 
end; 

然後我在設計時數據包中註冊TMyObjectATMyObjectBTMyObjectC

procedure Register(); 
begin 
    RegisterComponents('MyTestComponents', [ 
    TMyObjectA, 
    TMyObjectB, 
    TMyObjectC 
    ]); 

    //requires DesignIDE, uses DesignIntf 
    RegisterPropertyInCategory('Linkage', TMyObjectA, 'AlternativeValueSource'); 
end; 

我添加了4個對象,以一種形式:

MyObjectA1: TMyObjectA; 
MyObjectA2: TMyObjectA; 
MyObjectB1: TMyObjectB; 
MyObjectC1: TMyObjectC; 

選擇MyObjectA1,在AlternativeValueSource的對象檢查器的下拉列表中,我看到所有具有接口的對象。 (我期待只是MyObjectA1MyObjectA2它們實現IMyInterfaceA

enter image description here

回答

4

定義一個GUID到您的接口。

IMyInterfaceA = interface 
    ['{A5675798-F457-4E32-B0AA-608717CFD242}'] 
    function FGetValue() : Variant; 
    end; 

德爾福的IDE從他們的GUID(設計時間)識別接口。

+0

添加GUID後,所有事情都按要求開始工作。謝謝! – ExDev