如何通过Delphi将方法类型作为参数传递实现?

更新于
2026-10-04 18:17:56
2阅读来源:SEO资讯
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计452个文字,预计阅读时间需要2分钟。

我想要传递事件类型,如TNotifyEvent或TKeyPressEvent。如何声明方法参数来接受这些类型?

声明方法:procedure RegisterEventType(AEventType: ??);

这样编写:RegisterEventType(TNotifyEvent);RegisterEventType(TKeyPressEvent);

我想传递事件类型,如TNotifyEvent或TKeyPressEvent.

如何声明方法参数来接受这些类型?

procedure RegisterEventType(AEventType: ???)

这样编译:

RegisterEventType(TNotifyEvent) RegisterEventType(TKeyPressEvent)

指出TKeyPressEvent和TNotifyEvent明显不同:

TNotifyEvent = procedure(Sender: TObject) of object; TKeyPressEvent = procedure(Sender: TObject; var Key: Char) of object;

所以它不是我要传递的事件实例,它是类型.

为了给出一些上下文,这是实现:

procedure RegisterEventType(AEventType: ???; AFactory: TRedirectFactory) var vTypeInfo: PTypeInfo; begin vTypeInfo := TypeInfo(AEventType); Assert(vTypeInfo.Kind = tkMethod); vTypeInfo.Name <------ contains string 'TNotifyEvent' SetLength(fFactories, Length(fFactories)+1); fFactories[High(fFactories)].EventType := vTypeInfo.Name; fFactories[High(fFactories)].Factory := AFactory; end;

context:TRedirectFactory创建一个实现TNotifyEvent的IEventRedirect实例,用于重定向表单上的事件处理程序.每种支持的事件类型都有一个实现(TNotifyEvent,TKeyPressEvent …).
这允许集中记录和测量在事件处理程序中包含大量代码的表单.

因此,目的是不传递字符串’TNotifyEvent’,而是实际类型TNotifyEvent.

像hvd建议的那样,如果您的Delphi版本支持,可以使用Generics,例如:

type TEventTypeRegistrar<T> = class public class procedure Register(AFactory: TRedirectFactory); end;

class procedure TEventTypeRegistrar<T>.Register(AFactory: TRedirectFactory); var vTypeInfo: PTypeInfo; begin vTypeInfo := TypeInfo(T); Assert(vTypeInfo.Kind = tkMethod); SetLength(fFactories, Length(fFactories)+1); fFactories[High(fFactories)].EventType := vTypeInfo.Name; fFactories[High(fFactories)].Factory := AFactory; end;

TEventTypeRegistrar<TNotifyEvent>.Register(...); TEventTypeRegistrar<TKeyPressEvent>.Register(...);

或者:

type TEventTypeRegistrar = class public class procedure Register<T>(AFactory: TRedirectFactory); end;

class procedure TEventTypeRegistrar.Register<T>(AFactory: TRedirectFactory); var vTypeInfo: PTypeInfo; begin vTypeInfo := TypeInfo(T); Assert(vTypeInfo.Kind = tkMethod); SetLength(fFactories, Length(fFactories)+1); fFactories[High(fFactories)].EventType := vTypeInfo.Name; fFactories[High(fFactories)].Factory := AFactory; end;

TEventTypeRegistrar.Register<TNotifyEvent>(...); TEventTypeRegistrar.Register<TKeyPressEvent>(...);

本文共计452个文字,预计阅读时间需要2分钟。

我想要传递事件类型,如TNotifyEvent或TKeyPressEvent。如何声明方法参数来接受这些类型?

声明方法:procedure RegisterEventType(AEventType: ??);

这样编写:RegisterEventType(TNotifyEvent);RegisterEventType(TKeyPressEvent);

我想传递事件类型,如TNotifyEvent或TKeyPressEvent.

如何声明方法参数来接受这些类型?

procedure RegisterEventType(AEventType: ???)

这样编译:

RegisterEventType(TNotifyEvent) RegisterEventType(TKeyPressEvent)

指出TKeyPressEvent和TNotifyEvent明显不同:

TNotifyEvent = procedure(Sender: TObject) of object; TKeyPressEvent = procedure(Sender: TObject; var Key: Char) of object;

所以它不是我要传递的事件实例,它是类型.

为了给出一些上下文,这是实现:

procedure RegisterEventType(AEventType: ???; AFactory: TRedirectFactory) var vTypeInfo: PTypeInfo; begin vTypeInfo := TypeInfo(AEventType); Assert(vTypeInfo.Kind = tkMethod); vTypeInfo.Name <------ contains string 'TNotifyEvent' SetLength(fFactories, Length(fFactories)+1); fFactories[High(fFactories)].EventType := vTypeInfo.Name; fFactories[High(fFactories)].Factory := AFactory; end;

context:TRedirectFactory创建一个实现TNotifyEvent的IEventRedirect实例,用于重定向表单上的事件处理程序.每种支持的事件类型都有一个实现(TNotifyEvent,TKeyPressEvent …).
这允许集中记录和测量在事件处理程序中包含大量代码的表单.

因此,目的是不传递字符串’TNotifyEvent’,而是实际类型TNotifyEvent.

像hvd建议的那样,如果您的Delphi版本支持,可以使用Generics,例如:

type TEventTypeRegistrar<T> = class public class procedure Register(AFactory: TRedirectFactory); end;

class procedure TEventTypeRegistrar<T>.Register(AFactory: TRedirectFactory); var vTypeInfo: PTypeInfo; begin vTypeInfo := TypeInfo(T); Assert(vTypeInfo.Kind = tkMethod); SetLength(fFactories, Length(fFactories)+1); fFactories[High(fFactories)].EventType := vTypeInfo.Name; fFactories[High(fFactories)].Factory := AFactory; end;

TEventTypeRegistrar<TNotifyEvent>.Register(...); TEventTypeRegistrar<TKeyPressEvent>.Register(...);

或者:

type TEventTypeRegistrar = class public class procedure Register<T>(AFactory: TRedirectFactory); end;

class procedure TEventTypeRegistrar.Register<T>(AFactory: TRedirectFactory); var vTypeInfo: PTypeInfo; begin vTypeInfo := TypeInfo(T); Assert(vTypeInfo.Kind = tkMethod); SetLength(fFactories, Length(fFactories)+1); fFactories[High(fFactories)].EventType := vTypeInfo.Name; fFactories[High(fFactories)].Factory := AFactory; end;

TEventTypeRegistrar.Register<TNotifyEvent>(...); TEventTypeRegistrar.Register<TKeyPressEvent>(...);