delphi - Assign an existing method by its name from one Unit to a generic TMethod in another unit -


basically, have application retrieve information out of ini file , part of process includes extracting names -stored in ini- of procedures declared @ global scope inside unit.

i use following sql method:

myini.readstring('sql', substr + '_insert, '');  

substr type of data want, example "titles", prefixed type of sql procedure want, in case "_insert" therefore, request here seen as:

myini.readstring('sql', 'titles_insert', '');  

this retrieve appropriate sql procedure name "inserttitlessql" displayed inside ini thus:

[sql]     titles_insert=inserttitlessql 

i have unit lists sql procedures use. so:

unit usqllibrary;  interface   function inserttitlessql: string;  implementation  function inserttitlessql: string; begin   {     insert titles (english, afrikaans, key)     values (:english,             :afrikaans,             :key)   }   result := ''     +'insert titles (english, afrikaans, key) '     +'values (:english, '     +'        :afrikaans, '     +'        :key) '; end;  end. 

i've tried tgenericcontainer without success, i've tried methodaddress don't know how tell methodaddress @ other unit without object reference (form1.methodaddress() example) best result far this:

type    texec = procedure of object;  procedure tform1.button1click(sender: tobject); var   m : tmethod ;   e : texec ; begin   m.code := @test; // "test" procedure inside secondary unit.    e := texec(m);   e; end; 

what i'm trying sql procedure name want ( function inserttitlessql : string;) , assign generic method behaves in same way.

my team lead has said doesn't want edit usqllibrary; can't way , have thought go method's name instead. other suggestions welcome.

hope clear. sorry if it's not ( use of terminology not xd ). i'll try elaborate on queries best of knowledge if can.

you cannot use rtti enumerate procedures global scope. can enumerate methods, convert global procedures static class methods.

however, state team lead not want change usqllibrary. seems little short sighted in opinion. feel free tell him/her said so.

anyway, if cannot change usqllibrary cannot use rtti. you'll need construct own lookup table. use generic dictionary:

uses   system.generics.collections;  var   proctable: tdictionary<string, tproc>; 

instantiate in usual way. add functions @ program startup:

.... proctable.add('inserttitlessql', inserttitlessql); .... 

when need 1 , call this:

var   proc: tproc; .... if not proctable.trygetvalue(procname, proc)   raise eprocnotfound.createfmt(...); proc(); 

the default equality comparer used key case-sensitive default. may elect supply custom equality comparer compares keys without case sensitivity.


Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -