Einzelnen Beitrag anzeigen

Benutzerbild von s.h.a.r.k
s.h.a.r.k

Registriert seit: 26. Mai 2004
3.159 Beiträge
 
#7

AW: Generisches ToString für Enumerations

  Alt 30. Apr 2011, 15:34
Ha, ich habs

Delphi-Quellcode:
TLog = record
  class procedure AddEnum<T>(V: T); static;
end;

class procedure TLog.AddEnum<T>(V: T);
begin
  Writeln(GetEnumName(TypeInfo(T), PByte(@V)^));
end;
Der Tipp von himitsu war gut. Jetzt muss ich nur noch testen, ob das auch immer passen sollte. In meinen Fall passts jedenfalls. Aber man kann ja auch gewisse Werte für die Enum-Elemente definieren. Melde mich gleich wieder.

@daywalker: klar, so gehts auch

-- EDIT: So, habe mich jetzt nochmals ein wenig mit diesen Enumerations befasst und bin auf ein weiteres Problem gestoßen, welches dieses Zitat auf den Punkt bringt:
Zitat von http://delphi.wikia.com/wiki/GetEnumValue_Routine:
Be aware that Typeinfo will be lost when the enumeration is given different ordinal values. With the next enumeration example it's not possible to use GetEnumValue and will result in 'E2134 Type 'TTestType' has no type info'
Schade, aber man kann daran wohl nix ändern.
»Remember, the future maintainer is the person you should be writing code for, not the compiler.« (Nick Hodges)

Geändert von s.h.a.r.k (30. Apr 2011 um 16:07 Uhr)
  Mit Zitat antworten Zitat