AGB  ·  Datenschutz  ·  Impressum  







Anmelden
Nützliche Links
Registrieren
Zurück Delphi-PRAXiS Sprachen und Entwicklungsumgebungen Object-Pascal / Delphi-Language Delphi RTTI Attribute mit Variant: Invalid variant type

RTTI Attribute mit Variant: Invalid variant type

Ein Thema von Björn Ole · begonnen am 28. Okt 2012 · letzter Beitrag vom 28. Okt 2012
Antwort Antwort
Benutzerbild von Björn Ole
Björn Ole

Registriert seit: 11. Jul 2008
166 Beiträge
 
Delphi XE Professional
 
#1

RTTI Attribute mit Variant: Invalid variant type

  Alt 28. Okt 2012, 02:55
Delphi-Version: XE
Nabend!

Ich hantiere gerade mit Attributes und Variants rum und hänge fest. Hier mein Attribut:

Delphi-Quellcode:
type
  MyDefaultAttr = class(TCustomAttribute)
  private
    FDefaultValue: Variant;
  public
    constructor Create(DefaultValue: Variant); overload;
    property DefaultValue: Variant read FDefaultValue;
  end;

constructor MyDefaultAttr.Create(DefaultValue: Variant);
begin
  inherited Create;
  FDefaultValue := DefaultValue;
end;
Und hier wie ich sie benutze:

Delphi-Quellcode:
uses Rtti, TypInfo;

type
  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
  private
    FTestProp: boolean;
  public
    [MyDefaultAttr] // Funktioniert
    property FirstTestProp: boolean read FTestProp write FTestProp;
    [MyDefaultAttr(true)] // Fehler
    property SecondTestProp: boolean read FTestProp write FTestProp;
  end;

procedure TForm1.FormCreate(Sender: TObject);
var
  Context: TRttiContext;
  TypeInfo: TRttiType;
  Prop: TRttiProperty;
begin
  Context := TRttiContext.Create;
  try
    TypeInfo := Context.GetType(Self.ClassType);
    for Prop in TypeInfo.GetProperties do
      Prop.GetAttributes; // Exception
  finally
    Context.Free;
  end;
end;
Mit [MyDefaultAttr] gehts, sobald ich aber auf das Attribut eines Properties zugreifen möchte, das mit einem DefaultValue erstellt wurde [MyDefaultAttr(true)] , bekomm ich eine Exception:
First chance exception at $7524C41F. Exception class EVariantBadVarTypeError with message 'Invalid variant type'.

Hat da jemand eine Idee?


Beste Grüße
Björn
  Mit Zitat antworten Zitat
Benutzerbild von himitsu
himitsu

Registriert seit: 11. Okt 2003
Ort: Elbflorenz
43.014 Beiträge
 
Delphi 12 Athens
 
#2

AW: RTTI Attribute mit Variant: Invalid variant type

  Alt 28. Okt 2012, 02:40
Ich würde es mal als Bug definieren und diesen gibt es immernoch im XE3.
Am Besten solltest du mal im QC 'ne Meldung einreichen. (falls es nicht schon Eine gibt)


Das Problem ist nicht direkt der Variant, sondern der Weert, welchen du übergibst.

Vorallem scheint es da mit Typen ein Problem zu geben, welche der Variant standardmäßig selbst nicht unterstützt.

Zitat:
Delphi-Quellcode:
  // The data represented by a variant come in three flavors.
  //1) 'Inline data variant' is one whose data doesn't have to be allocated
  // and is actually contained within the payload area of the variant.
  // Since the data wasn't allocated, it doesn't have to be deallocated.
  // So to clear this type of variant we simply need to change it's VarType
  // to varEmpty. Some examples of this type of variant include:
  // varInteger, varDouble, varDate, etc.
  //2) 'External data variant' is one whose data has to be allocated. The
  // payload area of the variant only contains a reference to the allocated
  // data. But since we allocated the data we are responsible for
  // deallocating it. So to clear this type of variant we have to do more
  // than simply set it's VarType to varEmpty, the referenced data must
  // also be deallocated. Some examples of this type of variant include:
  // varString, varOleStr, varDispatch, varArray, etc.
  //3) 'By ref data' is a variant that simply points to externally managed
  // data. Since the data is managed by someone else we don't have to
  // deallocate the data when clearing this type of variant, we can simply
  // set the VarType to varEmpty. This type of variant's VarType will also
  // reflect the referenced data's VarType.
  //To quickly determine what we need to do for a certain VarType we have
  //created the following mask value. When 'AND'ed with the VarType of a
  //variant we can quickly determine which of the above three variant data
  //types is being cleared. This value is not perfect but there are only two
  //cases where it gets it wrong (varBoolean and varError) and those are
  //quickly dealt with when they come up.
  varDeepData = $BFE8;
varDeepData ist der Typ, welchen dein True-Wert intern besitzt.

Beim Auslesen und Übersetzen dieses Wertes gibt es dann den Fehler. Delphi-Referenz durchsuchenRTTI.TValue kann dadurch der Wert nicht zugewiesen werden. (System._VarCopy schlägt fehl)

Du kannst es gerne mal mit [MyDefaultAttr(2)] versuchen. [MyDefaultAttr(Integer(2))] funktioniert aber.
Leider funktioniert das Casten nach Boolean/ByteBool/WordBool/LongBool nicht. (wobei Variant den WordBool eigentlich kennt)
Garbage Collector ... Delphianer erzeugen keinen Müll, also brauchen sie auch keinen Müllsucher.
my Delphi wish list : BugReports/FeatureRequests
  Mit Zitat antworten Zitat
Benutzerbild von Björn Ole
Björn Ole

Registriert seit: 11. Jul 2008
166 Beiträge
 
Delphi XE Professional
 
#3

AW: RTTI Attribute mit Variant: Invalid variant type

  Alt 28. Okt 2012, 04:07
Ach so'n Mist: http://qc.embarcadero.com/wc/qcmain.aspx?d=104778

Dann muss ich wohl was drumrum basteln.

Danke.
  Mit Zitat antworten Zitat
Themen-Optionen Thema durchsuchen
Thema durchsuchen:

Erweiterte Suche
Ansicht

Forumregeln

Es ist dir nicht erlaubt, neue Themen zu verfassen.
Es ist dir nicht erlaubt, auf Beiträge zu antworten.
Es ist dir nicht erlaubt, Anhänge hochzuladen.
Es ist dir nicht erlaubt, deine Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks are an
Pingbacks are an
Refbacks are aus

Gehe zu:

Impressum · AGB · Datenschutz · Nach oben
Alle Zeitangaben in WEZ +1. Es ist jetzt 15:29 Uhr.
Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz