在C++中,我可以使用typeid运算符来检索任何多态类的名称:
const char* name = typeid( CMyClass ).name();
Run Code Online (Sandbox Code Playgroud)
const char* 只要存在相应的类,返回的指向的字符串将可用于我的程序.
多次调用会typeid(T).name()返回相同的指针值class T还是允许返回不同的指针?
我有RTTI TRttiMethod.Invoke,stdcall和const参数的问题:
obj := TClassRecordTest.Create;
try
b.a := 10; b.b := 100;
a.a := 1; a.b := 2;
writeln('b.a='+IntToStr(b.a)+' b.b='+IntToStr(b.b));
writeln;
writeln('call test1');
writeln('a.a='+IntToStr(a.a)+' a.b='+IntToStr(a.b));
r := VToRec(RTTICall(obj, 'Test1', @a, @b));
writeln('test1 r.a='+IntToStr(r.a)+' r.b='+IntToStr(r.b));
a.a := 2; a.b := 3;
writeln('call test2');
writeln('a.a='+IntToStr(a.a)+' a.b='+IntToStr(a.b));
r := VToRec(RTTICall(obj, 'Test2', @a, @b));
writeln('test3 r.a='+IntToStr(r.a)+' r.b='+IntToStr(r.b));
a.a := 3; a.b := 4;
writeln('call test3');
writeln('a.a='+IntToStr(a.a)+' a.b='+IntToStr(a.b));
r := VToRec(RTTICall(obj, 'Test3', @a, @b));
writeln('test3 r.a='+IntToStr(r.a)+' r.b='+IntToStr(r.b));
a.a := 4; a.b := 5;
writeln('call test4'); …Run Code Online (Sandbox Code Playgroud) 考虑这个简单的代码
{$APPTYPE CONSOLE}
uses
Rtti,
SysUtils;
type
{$M+}
TFoo = class
strict private
class var Field1 : Integer;
field2 : Integer;
private
field3 : Integer;
class var Field4 : Integer;
end;
Var
ctx : TRttiContext;
f : TRttiField;
begin
try
ctx:=TRttiContext.Create;
for f in ctx.GetType(TFoo).GetFields do
Writeln(f.Name);
Writeln('Done');
readln;
except
on E: Exception do
Writeln(E.ClassName, ': ', E.Message);
end;
end.
Run Code Online (Sandbox Code Playgroud)
运行此时,仅field3列出该列表.似乎哪个RTTI不支持字段,strict private或者class var,问题是Is possible access a strict private field of a delphi class …
我使用Boost :: iostreams同时写入我的控制台和文件.当我使用eclipse进行调试时(当然使用gdb),我会收到一条警告,说明我在Boost :: iostreams中使用的某个类没有找到RTTI符号.
这是重现问题的最小代码.
#ifndef BOOST_IO_STREAM_H_
#define BOOST_IO_STREAM_H_
#include <fstream>
#include <boost/iostreams/tee.hpp>
#include <boost/iostreams/stream.hpp>
using boost::iostreams::tee_device;
using boost::iostreams::stream;
typedef tee_device<std::ostream, std::ofstream> TeeDevice;
typedef stream<TeeDevice> TeeStream;
#endif /* BOOST_IO_STREAM_H_ */
int
main()
{
/* A config file to output experiment details */
std::string self_filename = "./experimentconfig.txt";
std::ofstream fconfig(self_filename.c_str());
TeeDevice my_tee(std::cout, fconfig);
TeeStream cool_cout(my_tee);
cool_cout << "Output to file and console during experiment run" << std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
当我TeeStream cool_cout(my_tee);在调试期间越线时,我收到以下警告:
warning: RTTI symbol not found for class …Run Code Online (Sandbox Code Playgroud) 我的问题是在运行时如何加载类信息?
有人打电话时instanceof是考虑RTTI还是反思?还是取决于实际情况?
我无法在任何地方找到这个看似简单的问题的答案.
以下C++函数是否使用RTTI?它当然不必,但我想知道是否有保证在编译时确定typeid.
template <typename T>
const char *getName()
{
return typeid(T).name(); // Resolved at compile time?
}
Run Code Online (Sandbox Code Playgroud) 是否可以以及如何在运行时创建自定义属性并将其附加到字段?
uses
System.SysUtils,
System.Classes,
System.Rtti;
type
MyAttribute = class(TCustomAttribute)
private
fCaption: string;
public
constructor Create(const aCaption: string);
property Caption: string read fCaption write fCaption;
end;
TFoo = class(TPersistent)
public
[MyAttribute('Title')]
Bar: string;
Other: string;
end;
constructor MyAttribute.Create(const aCaption: string);
begin
fCaption := aCaption;
end;
procedure CreateAttributes(Typ: TRttiType);
var
Field: TRttiField;
MyAttr: MyAttribute;
begin
for Field in Typ.GetFields do
begin
if Length(Field.GetAttributes) = 0 then
begin
MyAttr := MyAttribute.Create('Empty');
// how to attach created attribute to Field ???
end;
end;
end; …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用RTTI使用Text-property来概括可视组件的内容验证,但是当我尝试将字符串值传递给TRttiMethod.Invoke时,我收到消息"Invalid Typecast".(实际上是"UngültigeTypumwandlung",但我猜,这是一个合适的翻译.)
假设所有传递的对象都是完美的,下面的代码将被删除所有安全措施,断言等.
procedure ValidateTextFieldAndSetFocus(const Field: TObject; const Validator: TObject; const errorStates: array of TStringValidationResult; const sErrorMessage: string);
var
context : TRttiContext;
objField : TRttiType;
objValid : TRttiType;
prop : TRttiProperty;
execute : TRttiMethod;
I : Integer;
validResult : TStringValidationResult;
value : TValue;
begin
context := TRttiContext.Create;
objField := context.GetType(Field.ClassInfo);
objValid := context.GetType(Validator.ClassInfo);
prop := objField.GetProperty('Text');
value := prop.GetValue(Field);
execute := objValid.GetMethod('Execute');
for I := 0 to High(errorStates) do
if execute.Invoke(Validator,[value]).TryAsType<TStringValidationResult>(validResult) then
if validResult = errorStates[I] then
begin
SetFocusIfCan(Field);
raise …Run Code Online (Sandbox Code Playgroud) 我看过一本关于C++的书,提到使用静态转换导航继承层次结构比使用动态转换更有效.
例:
#include <iostream>
#include <typeinfo>
using namespace std;
class Shape { public: virtual ~Shape() {}; };
class Circle : public Shape {};
class Square : public Shape {};
class Other {};
int main() {
Circle c;
Shape* s = &c; // Upcast: normal and OK
// More explicit but unnecessary:
s = static_cast<Shape*>(&c);
// (Since upcasting is such a safe and common
// operation, the cast becomes cluttering)
Circle* cp = 0;
Square* sp = 0;
// Static Navigation …Run Code Online (Sandbox Code Playgroud) 这可能是一个"不",但有什么方法可以使用Delphi的RTTI(旧学校或2010扩展RTTI)传入一个包含类型名称的字符串,特别是枚举的名称类型,并让它给我这种类型的PTypeInfo?我查看了RTTI.pas和TypInfo.pas,我没有看到任何可以做到这一点的函数,但我可能错过了一些东西.
我在找什么:
var
info: PTypeInfo;
begin
info := GetTypeInfoFromName('TComponentStyle');
end;
Run Code Online (Sandbox Code Playgroud)
或类似的东西.事实是,枚举类型的名称将被传入; 在编译时不知道.
rtti ×10
delphi ×5
c++ ×3
delphi-xe ×2
attributes ×1
casting ×1
delphi-xe3 ×1
dynamic-cast ×1
eclipse-cdt ×1
gdb ×1
instanceof ×1
java ×1
reflection ×1
static-cast ×1
templates ×1
terminology ×1
typeid ×1
typeinfo ×1
visual-c++ ×1