C#, Dynamic Functions, Threads

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?Utf-8?B?QWhtYWQ=?=

    C#, Dynamic Functions, Threads

    Hi,

    Is it possible to do something like the the following:

    IBaseClass objClass = new LoadClassFromDL L( "Module.DLL ");
    ThreadStart objThreadStart= new
    (objClass.GetFu nctionFromName( "GetString" ))

    Thread objThread = new Thread (objThreadStart )
    objThread.Start ();

    bearing in mind the function 'GetFunctionFro mName' doesn't actually exist,
    and I don't know how to implement it (I've sorted out the 'LoadClassFromD LL'
    function).

    Thanks in Advance.

    Regards,

    Ahmad

  • Jon Skeet [C# MVP]

    #2
    Re: C#, Dynamic Functions, Threads

    On Jul 10, 2:28 pm, Ahmad <Ah...@discussi ons.microsoft.c omwrote:
    Is it possible to do something like the the following:
    >
        IBaseClass objClass = new LoadClassFromDL L( "Module.DLL ");
        ThreadStart objThreadStart= new
    (objClass.GetFu nctionFromName( "GetString" ))
    >
        Thread objThread = new Thread (objThreadStart )
        objThread.Start ();
    >
    bearing in mind the function 'GetFunctionFro mName' doesn't actually exist,
    and I don't know how to implement it (I've sorted out the 'LoadClassFromD LL'
    function).
    Yes, that's basically possible. Look at Type.GetMethod and
    MethodInfo.Invo ke. If you're still stuck after a bit of looking, let
    me know and I'll write up a short example program. (I'd do it now, but
    I really ought to get on with some work...)

    Jon

    Comment

    • =?Utf-8?B?QWhtYWQ=?=

      #3
      Re: C#, Dynamic Functions, Threads

      Hi,

      Originally I had:

      IModule objModule =
      ModuleManager.G etInstance(@"C: \projects\bin\l ibrary\Module.d ll");
      Type objType = objModule.GetTy pe();
      MethodInfo objMethodInfo = objType.GetMeth od("GetString") ;
      Thread objThread = new Thread(objMetho dInfo);

      but that doesn't work because the Thread Constructor is expecting a method
      rather than an object (in this case MethodInfo). Even using:

      IModule objModule =
      ModuleManager.G etInstance(@"C: \projects\bin\l ibrary\Module.d ll");

      Type objType = objModule.GetTy pe();


      MethodInfo objMethodInfo = objType.GetMeth od("GetString") ;
      ThreadStart objThreadStart= new (objMethodInfo)
      Thread objThread = new Thread(objThrea dStart);

      won't work because [yet again] ThreadStart Constructur expecting a method
      rather than an object.

      Thanks in Advance.

      Regards,

      Ahmad

      "Jon Skeet [C# MVP]" wrote:
      On Jul 10, 2:28 pm, Ahmad <Ah...@discussi ons.microsoft.c omwrote:
      Is it possible to do something like the the following:

      IBaseClass objClass = new LoadClassFromDL L( "Module.DLL ");
      ThreadStart objThreadStart= new
      (objClass.GetFu nctionFromName( "GetString" ))

      Thread objThread = new Thread (objThreadStart )
      objThread.Start ();

      bearing in mind the function 'GetFunctionFro mName' doesn't actually exist,
      and I don't know how to implement it (I've sorted out the 'LoadClassFromD LL'
      function).
      >
      Yes, that's basically possible. Look at Type.GetMethod and
      MethodInfo.Invo ke. If you're still stuck after a bit of looking, let
      me know and I'll write up a short example program. (I'd do it now, but
      I really ought to get on with some work...)
      >
      Jon
      >

      Comment

      • Jon Skeet [C# MVP]

        #4
        Re: C#, Dynamic Functions, Threads

        Ahmad <Ahmad@discussi ons.microsoft.c omwrote:
        IModule objModule =
        ModuleManager.G etInstance(@"C: \projects\bin\l ibrary\Module.d ll");
        Type objType = objModule.GetTy pe();
        MethodInfo objMethodInfo = objType.GetMeth od("GetString") ;
        Thread objThread = new Thread(objMetho dInfo);
        >
        but that doesn't work because the Thread Constructor is expecting a method
        rather than an object (in this case MethodInfo). Even using:
        >
        IModule objModule =
        ModuleManager.G etInstance(@"C: \projects\bin\l ibrary\Module.d ll");
        >
        Type objType = objModule.GetTy pe();
        >
        >
        MethodInfo objMethodInfo = objType.GetMeth od("GetString") ;
        ThreadStart objThreadStart= new (objMethodInfo)
        Thread objThread = new Thread(objThrea dStart);
        >
        won't work because [yet again] ThreadStart Constructur expecting a method
        rather than an object.
        Well, it's expecting a delegate.

        You could change it to:

        ThreadStart threadStart = delegate { objMethodInfo.I nvoke(null); }

        --
        Jon Skeet - <skeet@pobox.co m>
        Web site: http://www.pobox.com/~skeet
        Blog: http://www.msmvps.com/jon_skeet
        C# in Depth: http://csharpindepth.com

        Comment

        Working...