标签: rtti

如何最好地在DWScript中公开类实例

我正在使用优秀的Pascal DWScript整合内置脚本功能.我还使用以下命令将自己的Delphi端类定义(TDemo)添加到DWScript:

dwsUnit.ExposeRTTI( TDemo.ClassInfo  )
Run Code Online (Sandbox Code Playgroud)

这只是工作,是快速添加属性和方法的好方法.

我也希望以类似的方式添加现有实例,因此我创建了类型为TDemo的实例FDemo,然后执行:

   dwsUnit.ExposeInstanceToUnit( 'Demo', 'TDemo', FDemo );
Run Code Online (Sandbox Code Playgroud)

这看起来很有前途,但是我从未初始化的单位表中得到一个AV.我也查看了SVN源的单元测试代码,看看这个功能的使用但无济于事.任何人都可以指出我应该添加/更改的内容吗?

delphi rtti dwscript

6
推荐指数
1
解决办法
816
查看次数

如何获取TRTTIParamter的默认值

我有这门课:

TMyClass = class
public
  function DoSomethingNice(const Value: string = 'Yes please!'): Boolean;
end;
Run Code Online (Sandbox Code Playgroud)

现在,使用RTTI,是否可以获取方法DoSomethingNice的参数的默认值?如果是这样,怎么样?

我最感兴趣的是D2010解决方案,但XE也会这样做.

delphi rtti delphi-2010 delphi-xe

6
推荐指数
1
解决办法
476
查看次数

TRttiMethod.Invoke函数在重载方法中不起作用?

我正在使用TRttiMethod.Invoke函数创建类的实例,但是当构造函数或方法重载时,rtti不会调用正确的方法.

我写了一个示例应用来说明我的问题.

program ProjectFoo;

{$APPTYPE CONSOLE}

{$R *.res}

uses
  Rtti,
  System.SysUtils;

type
  TFoo=class
  public
    constructor Create(Value :  Integer);overload;
    constructor Create(const Value :  string);overload;
    function Bar(value : integer) : Integer; overload;
    function Bar(const value : string) : string; overload;
  end;

{ TFoo }

constructor TFoo.Create(Value: Integer);
begin
   Writeln(Value);
end;

function TFoo.Bar(value: integer): Integer;
begin
   Writeln(Value);
   Result:=value;
end;

function TFoo.Bar(const value: string): string;
begin
   Writeln(Value);
   Result:=value;
end;


constructor TFoo.Create(const Value: string);
begin
   Writeln(Value);
end;

var
 c : TRttiContext;
 t : …
Run Code Online (Sandbox Code Playgroud)

delphi rtti delphi-xe2

6
推荐指数
1
解决办法
3731
查看次数

如何在运行时设计器中隐藏继承的已发布属性?

我正在使用属性检查器(例如JVCL库中的漂亮的JvInspector),它很好地让我查看和编辑我的类'TMyClass'的已发布属性.TMyClass来自TFrame并且有一些已发布的属性是我需要查看和编辑的唯一属性.当然TFrame有很多VCL属性,都是已发布的,我也看到了.

如何抑制祖先属性RTTI并留下我自己发布的属性?我正在使用XE3所以世界是我的牡蛎......也许吧.

delphi properties rtti delphi-xe3

6
推荐指数
1
解决办法
2473
查看次数

RTTI 是如何工作的?

我对 C++ 中的 RTTI 机制有些困惑。

假设有类 A 和继承自 A 的类 B。 现在考虑以下代码:

B* b = new B();
A* a = dynamic_cast<A*>(b);
Run Code Online (Sandbox Code Playgroud)

我知道具有虚方法的多态类有虚表和 vptr,但我认为指针只提供有关虚函数的信息。程序如何在运行时知道 b 的类型,使用 vptr 和 vtables?

c++ polymorphism rtti

6
推荐指数
1
解决办法
1606
查看次数

TValue string < - >布尔来回

我正和TValue一起玩

我在一个空白项目中编写了这段代码:

uses
  RTTI;

procedure TForm1.FormCreate(Sender: TObject);
var
  s: string;
  b: Boolean;
begin
  s := TValue.From<Boolean > (True).ToString;
  b := TValue.From<string > (s).AsType<Boolean>;
end;
Run Code Online (Sandbox Code Playgroud)

但我无法从字符串转换回布尔值; 我在第二行得到一个无效的Typecast异常.

我正在使用Delphi XE,但它与Delphi Xe6中的结果相同,这导致我得出结论:我使用的是TValue错误.

那么请问我做错了什么.

delphi rtti delphi-xe

6
推荐指数
2
解决办法
1798
查看次数

将RTTI与从外部库派生的对象一起使用

我正在使用外部库,需要创建一个观察者模式,其中观察者来自属于该库的对象.我不想从库中更改基类,同时我必须使用这个不可更改的基类的引用/指针列表.除此之外,库构造了对象列表,我需要从中筛选出适合观察者的对象.

我写的代码大致相当于:

#include <iostream>
#include <vector>
#include <memory>

// This class is from an external library which I don't want to chagne
class BaseFromLibrary {
  public:
    virtual ~BaseFromLibrary() {}
};

class BaseOfObserver {
  public:
    void notify() { std::cout << "What-ho!\n"; };
};

class Observer : public BaseFromLibrary, public BaseOfObserver {};

class Subject {
  public:
    std::vector<std::shared_ptr<Observer>> observers;
    void notifyObervers() {
        for (auto &o : observers)
            (*o).notify();
    }
};

int main() {
    // This list is constructed by the library and I …
Run Code Online (Sandbox Code Playgroud)

c++ design-patterns rtti

6
推荐指数
1
解决办法
138
查看次数

实现接口在运行时有什么好处?

我正在尝试了解TVirtualInterface类.

{$APPTYPE CONSOLE}

uses
  SysUtils, Rtti;

type
  ISpecificInterface = interface(IInvokable)
    ['{281D8B97-397E-430A-895A-9CA4E1F5FB5F}']
    procedure SpecificProcedure;
  end;

procedure AProcedure(Method: TRttiMethod; const Args: TArray<TValue>;
  out Result: TValue);
begin
  Writeln(Method.ToString);
end;

var
  ISpecificInterfaceInstance: ISpecificInterface;

begin
  ISpecificInterfaceInstance := TVirtualInterface.Create
    (TypeInfo(ISpecificInterface), AProcedure) as ISpecificInterface;

  ISpecificInterfaceInstance.SpecificProcedure;

end. // TVirtualInterface ref. counter is decremented
Run Code Online (Sandbox Code Playgroud)

在运行时实现接口有什么好处?

空间的用途是什么?

delphi oop rtti

6
推荐指数
1
解决办法
209
查看次数

在运行时从指向基类的指针获取对象的类型

我正在使用一个类库,其中所有类都直接或间接地从基类派生Base并具有名称。该库提供了一种按名称搜索对象的工具,该名称将返回一个Base*.

有没有办法找到返回对象的类型,而无需dynamic_cast像我在以下示例中那样使用s检查所有可能性?如果可能的话,我想避免这种情况,因为派生类具有模板参数,这带来了很多可能性。

如果我至少能够在不知道模板类型的情况下找出类类型(T1或者T2,在下面的示例中),那也很好,即。做类似的事情dynamic_cast<T1<i_dont_care>*>

#include <iostream>

using namespace std;

class Base {
public:
    virtual ~Base() {}
};

template <typename T> class T1 : public Base {};
template <typename T> class T2 : public Base {};

Base *find_by_name() {
    return new T2<int>();
}

int main() {
    Base *b = find_by_name();

    if (T1<int> *p = dynamic_cast<T1<int>*>(b)) {
        cout << "T1<int>" << endl;
        // do something meaningful with p
    } else if …
Run Code Online (Sandbox Code Playgroud)

c++ dynamic rtti

6
推荐指数
2
解决办法
6383
查看次数

为什么在 VCL 控件上调用 TRttiContext.GetType 时某些属性会重复?

当在 VCL 控件上调用 TRttiContext.GetType 时,为什么某些属性会重复(例如ActionAlign),而其他属性则不会 ( AlignWithMargins)?

uses
  System.RTTI,
  System.Generics.Collections,
  System.Generics.Defaults;

//....

procedure TForm11.btnShowPropertiesClick(Sender: TObject);
var
  R: TRttiContext;
  Props: TArray<TRttiProperty>;
  Prop : TRttiProperty;
begin
  memo1.Clear;
  R := TRttiContext.Create;
  Props := R.GetType(Sender.ClassType).GetProperties;

  //Sort properties by name
  TArray.Sort<TRttiProperty>(props,
    TComparer<TRttiProperty>.Construct(
      function(const Left, Right: TRttiProperty): Integer
      begin
        result := CompareText(Left.Name, Right.Name);
      end
    )
  );

  for prop in Props do
  begin
    try
      Memo1.Lines.Add(
         Prop.Name + ' : ' +
         Prop.PropertyType.ToString + ' = ' +
         Prop.GetValue(Sender).ToString);
    except
      Memo1.Lines.Add(Prop.Name + ' …
Run Code Online (Sandbox Code Playgroud)

delphi rtti delphi-10.4-sydney

6
推荐指数
2
解决办法
192
查看次数