0

这是对此处发布的答案的后续问题:

https://stackoverflow.com/a/55588208/2918172

它有一个解决方法,用于解决在调用方法时私有字段未刷新的TJsonTextReader代码中的错误。在 System.JSON.Readers.hpp 中定义如下:FCharsRewindFChars

typedef System::DynamicArray<System::WideChar> _TJsonTextReader__1;

_TJsonTextReader__1 FChars;

我的问题是如何在 C++Builder 中实现相同的功能(使用 clang 编译器)。我以前没有使用过RTTI。这是我到目前为止所拥有的:

//workaround for RSP-24517
// Get class RTTI
TRttiContext context;
TRttiInstanceType *cls = dynamic_cast<TRttiInstanceType*>(context.GetType(__delphirtti(TJsonTextReader)));

if (cls)
{
    // Get field RTTI
    TRttiField *field = cls->GetField("FChars");
    if (field)
    {
        TValue val1, val2, val3;
        val1 = field->GetValue(AReader);        

        // 1st attempt: same as original delphi solution:
        val1.SetArrayElement(0, '\0');   // Cases "calling a protected constructor of class 'System::Rtti::TValue' " compiler error        

        // 2nd attempt: create new TValue as WideChar directly
        val2 = System::WideChar(0);      // builds but causes typecast exception when SetArrayElement is called
        val1.SetArrayElement(0, val2);

        // 3rd attempt: create new TValue by capturing type of existing array element and modifying the value
        val2 = val1.GetArrayElement(0);
        int tempVal = 0;
        System::Rtti::TValue::Make(&tempVal, val2.TypeData, val3);    // Causes "no matching function for call to 'Make' " compiler error
        val1.SetArrayElement(0, val3);                    
    }
}

如何FChars用空字符替换 的第一个元素?

4

0 回答 0