有关RTTI,请问如何从一个控件中通过一个事件名称得到该事件的类型信息PTypeInfo(有一定难度)?
在此虚心请教各位达人。
比如给定TButton,及名称'OnClick',如何得到OnClick对应的事件的PTypeInfo?
函数定义如下:
function GetEventTypeInfo(ComponentClass:TPersistentClass; EventName: string):PTypeInfo;
begin
//??? 这里如何实现?
end;
因为只知道一个类名和一个事件名,而不知道具体的事件类型,所以不能直接使用TypeInfo(TNotifyEvent)这样的方式直接取得事件的类型信息,而要通过类和事件名来取得,事件是published的。  

解决方案 »

  1.   

    调用形式:GetEventTypeInfo(TButton,'OnClick');
      

  2.   

    通过分析Typinfo.pas,初步实现了如下代码,希望有达人能进一步。
    function GetEventTypeInfo(ComponentClass:TPersistentClass; EventName: string):PTypeInfo;
    var
      p:PPropInfo;
    begin
      p:= GetPropInfo(ComponentClass,EventName,tkMethods);
      if p <> nil then
      result := p^.PropType^;
    end;
      

  3.   

    就是这样用的再进一步就可以动态创建、设置属性方法
    http://blogs.codegear.com/abauer/2007/06/14/36043
      

  4.   

    function   GetEventTypeInfo(ComponentClass:TPersistentClass;   EventName:   string):PTypeInfo; 
    var 
        p:PPropInfo; 
    begin 
        p:=   GetPropInfo(ComponentClass,EventName,tkMethods); 
        if   p   <>   nil   then 
        result   :=   p^.PropType^; 
    end;-------------------------------------------------
    嗯,我就是这样用的。
      

  5.   

    TypInfo.GetTypeData()也可以获得很多有用的东西!
      

  6.   

    我简单的解释一下用途。
    取得PTypeInfo有什么用呢=>>
    当然有用,在delphi7中的DesignEditors.pas中有一个函数:
    procedure RegisterPropertyEditor(PropertyType: PTypeInfo; ComponentClass: TClass;
      const PropertyName: string; EditorClass: TPropertyEditorClass);
    其中第一个参数PropertyType需要传入的就是一个PTypeInfo类型的。简单的解释下用途,
    我要用GetEventTypeInfo动态的取得一个类(不是类的实例)的published的事件的PTypeInfo,如果该类存在这个事件,就传到RegisterPropertyEditor动态注册一个该事件的PropertyEditor。
    比如GetEventTypeInfo(TButton,'OnClickXXX'),GetEventTypeInfo(TButton,'OnClick')等等,其中两个参数都是动态的,未知的,如果返回的不是Nil,那就说明这个类有这个事件,我就把它给注册了~~