此代码在最后一行Invalid typecast
处引发prop->SetValue(control, value)
。
我假设我错误地投射了"MyString"
。什么是正确的方法?
for (int i = 0; i < MyForm->ControlCount; i++) {
TControl *control = MyForm->Controls[i];
if (control->Name == "MyTEdit") {
TRttiContext ctx;
TRttiType *type = ctx.GetType(control->ClassInfo());
TRttiProperty *prop = type->GetProperty(L"Text");
TValue value = TValue::From("MyString");
prop->SetValue(control, value);
}
}
应该遍历MyForm
中的所有控件,直到找到带有TEdit
的Name
的MyTEdit
,然后将框中的文本更改为MyString
。
我的代码基于this答案。不幸的是,它没有提供一个将String
文字转换为TValue
的示例,所以我很茫然。
阅读了this问题的答案后,我将TValue value = TValue::From("MyString");
更改为:
String myString = "MyString";
TValue value = TValue::From<UnicodeString>(myString);
现在出现以下错误:
[ilink32 Error] Error: Unresolved external 'System::Rtti::TValue __fastcall System::Rtti::TValue::From<System::UnicodeString>(System::UnicodeString)' referenced from UNIT1.OBJ
我已经在Unit1.cpp
的顶部包括了这个:
#pragma explicit_rtti
#include <System.Rtti.hpp>
所以我不明白为什么会这样说。
当我从32位切换到64位时,问题消失了。
[TValue::From<T>()
是一种Delphi泛型方法,并且在C ++中使用泛型已记录了链接器问题:
How to Handle Delphi Generics in C++。
请尝试using the assignment operator=
,因为operator=
具有TValue
的赋值运算符:
UnicodeString
String myString = "MyString";
TValue value;
value = myString;