type
    TIn=Class(TObject)
    private
      fCap,fText:String;
      fDefault:Variant;
    public
      procedure SetCap(sCap:String);
      procedure SetText(sText:String);
      procedure SetDefault(v:Variant);
      function GetValue:Variant;
    published
      property Caption:String read fCap write SetCap;
      property Text:String read fText write SetText;
      property Defau:Variant read fDefault write SetDefault;
      property Value:Variant read GetValue;    end;
in:=TIn.Create;
为什么SetPropValue(in,'Caption','测试');
总是报错:raised exception class EPropertyError with message "Property Caption does not exists"
提示Caption属性不存在啊?

解决方案 »

  1.   

    改成从TPersistent继承,或在编译时加上{$M+}
    TObject没有生成rtti功能
      

  2.   

    楼上正解:)Type Switch
    Syntax {$M+} or {$M-}
    {$TYPEINFO ON} or {$TYPEINFO OFF}
    Default {$M-}
    {$TYPEINFO OFF}
    Scope Local
    The $M switch directive controls generation of runtime type information (RTTI). When a class is declared in the {$M+} state, or is derived from a class that was declared in the {$M+} state, the compiler generates runtime type information for fields, methods, and properties that are declared in a published section. If a class is declared in the {$M-} state, and is not derived from a class that was declared in the {$M+} state, published sections are not allowed in the class. Note that if a class is forward declared, the first declaration of the class must be declared with the $M switch.When the $M switch is used to declare an interface, the compiler generates runtime type information for all properties and methods. That is, for interfaces, all members are treated as if they were published.Note: The TPersistent class defined in the Classes unit of the component library is declared in the {$M+} state, so any class derived from TPersistent will have RTTI generated for its published sections. The component library uses the runtime type information generated for published sections to access the values of a component's properties when saving or loading form files. Furthermore, the IDE uses a component's runtime type information to determine the list of properties to show in the Object Inspector.Note: The IInvokable interface defined in the System unit is declared in the {$M+} state, so any interface derived from IInvokable will have RTTI generated. The routines in the IntfInfo unit can be used to retrieved the RTTI.There is seldom, if ever, any need for an application to directly use the $M compiler switch.program Project1;{$APPTYPE CONSOLE}uses
      SysUtils, TypInfo, Classes;
    {$M+}
    type
      T = class//(TPersistent)
      private
        FCaption: string;
      published
        property Caption: string read FCaption write FCaption;
      end;
    {$M-}
    var
      t1: T;
    begin
      t1:=T.Create;
      SetPropValue(t1, 'Caption', '测试');
      WriteLn(t1.Caption);
      t1.Free;
      ReadLn;
    end.