即使使用正确的{$ METHODINFO}指令声明给定的类,GetPropInfo是否有可能返回nil.
type
...
...
{$METHODINFO ON}
TMyClass = class
private
fField: integer;
published
property Field: integer read fField write fField;
end;
{$METHODINFO OFF}
...
...
procedure TestRTTI;
begin
assert(assigned(GetPropInfo(TMyClass, 'Field')), 'WTF! No RTTI found!');
end;
Run Code Online (Sandbox Code Playgroud) 如何在C++中模拟C#typeof-command行为?
C#示例:
public static PluginNodeList GetPlugins (Type type)
{
...
}
Run Code Online (Sandbox Code Playgroud)
呼叫:
PluginManager.GetPlugins (typeof(IPlugin))
Run Code Online (Sandbox Code Playgroud)
如何使用C++实现这一点?也许QT或Boost库提供了解决方案?
如果你想以一种从文件(.so或.dll)加载那些类型的对象的方式实现.GetPlugins(...)的情况呢?
为了这个问题,这被大大简化了.说我有一个层次结构:
struct Base {
virtual int precision() const = 0;
};
template<int Precision>
struct Derived : public Base {
typedef Traits<Precision>::Type Type;
Derived(Type data) : value(data) {}
virtual int precision() const { return Precision; }
Type value;
};
Run Code Online (Sandbox Code Playgroud)
我想要一个带有签名的非模板函数:
Base* function(const Base& a, const Base& b);
Run Code Online (Sandbox Code Playgroud)
函数结果的具体类型与任何一个类型相同a且b具有更大的类型Precision; 像下面的伪代码:
Base* function(const Base& a, const Base& b) {
if (a.precision() > b.precision())
return new A( ((A&)a).value + A(b.value).value );
else if (a.precision() < b.precision()) …Run Code Online (Sandbox Code Playgroud) 我想使用TValue将字符串转换为枚举类型,我用谷歌搜索但我没有找到如何做到这一点.
type
TEnumTest = (etFirst, etSecond);
var
D: TEnumTest;
begin
D := StrToENumTest('etFirst');
end;
function StrToEnumTest(pStr:String):TEnumTest;
var
V: TValue;
begin
V := TValue.From<String>(pstr);
Result := V.AsType<TEnumTest>;
end;
Run Code Online (Sandbox Code Playgroud)
它不起作用.那肯定是我看不到的蠢事 - 但我没有找到它.我做错了什么?
我知道如何使用GetEnumValue.
编辑:@Warren,它在这里,因为这更容易发布代码:
TEnumUtils = class
class function GetAs<T>(pValor: String): T;
end;
class function TEnumUtils.GetAs<T>(pValor: String): T;
var
Tipo: PTypeInfo;
Temp: Integer;
PTemp: Pointer;
begin
Tipo := TypeInfo(T);
Temp := GetEnumValue(Tipo, pValor);
PTemp := @Temp;
Result := T(PTemp^);
end;
Run Code Online (Sandbox Code Playgroud)
用法:
type
TEnumTest = (etFirst, etSecond);
var
D: TEnumTest;
begin
D := …Run Code Online (Sandbox Code Playgroud) 如何在Delphi记录中提取有关方法的RTTI信息?是否可以使用新的Rtti装置?
我目前正在使用此代码,但没有列出任何内容.我错过了什么?
program ListAttrs;
{$APPTYPE CONSOLE}
uses
Rtti,
SysUtils;
type
TPerson = class
private
FName: String;
FAge: Integer;
public
[NonEmptyString('Must provide a Name')]
property Name : String read FName write FName;
[MinimumInteger(18, 'Must be at least 18 years old')]
[MaximumInteger(65, 'Must be no older than 65 years')]
property Age : Integer read FAge write FAge;
end;
procedure test;
var
ctx : TRttiContext;
lType : TRttiType;
lAttribute: TCustomAttribute;
lProperty : TRttiProperty;
begin
ctx := TRttiContext.Create;
lType := ctx.GetType(TPerson);
for lProperty in lType.GetProperties …Run Code Online (Sandbox Code Playgroud) 如果我调用typeid并检索返回的地址type_info:
const type_info* info = &( typeid( Something ) );
Run Code Online (Sandbox Code Playgroud)
返回的对象的生命周期是什么typeid,指向该对象的指针有效期是多久?
我有几个类的第三方组件(我无法修改),现在我需要访问implementation该单元部分中声明的一些类,问题:Is possible get rtti info for types declarated in the implementation part of a external unit?如果不存在可能存在另一种方式来访问这些类型?我知道单位的范围,而且执行声明是私有的,只能在特定单位内使用.但也许存在一些黑客攻击.
我有继承自A的B类.
class A
{
};
class B : public A
{
};
Run Code Online (Sandbox Code Playgroud)
我有三个对象.
A* a = new A();
A* a2 = new B();
B* b = new B();
Run Code Online (Sandbox Code Playgroud)
我想如果检查a是A类型的对象,a2是B类型的对象(不是A),b是B类型的对象.
我试过打字比较,但它没有给我正确的答案.
cout << (typeid(*a) == typeid(A)) << endl; // -> 1
cout << (typeid(*a2) == typeid(A)) << endl; // -> 1
cout << (typeid(*b) == typeid(A)) << endl; // -> 0
cout << (typeid(*a) == typeid(B)) << endl; // -> 0
cout << (typeid(*a2) == typeid(B)) << endl; // -> 0 …Run Code Online (Sandbox Code Playgroud) 如何在运行时在Go中获取函数参数,我只知道如何获取函数名称:
pc, file, line, ok := runtime.Caller(2)
rt := runtime.FuncForPC(pc)
return rt.Name() // Foo
Run Code Online (Sandbox Code Playgroud)
我需要的是这样的:
Foo(1,2,3)
// Foo_1_2_3
Run Code Online (Sandbox Code Playgroud) rtti ×10
delphi ×5
c++ ×4
delphi-2010 ×3
typeid ×3
delphi-xe ×2
attributes ×1
c++11 ×1
go ×1
methods ×1
polymorphism ×1
records ×1
tvalue ×1
typeinfo ×1
typeof ×1
visual-c++ ×1