Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. how to call DLL function in generic method?

how to call DLL function in generic method?

Scheduled Pinned Locked Moved C#
comhelptutorialquestion
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    vicky457
    wrote on last edited by
    #1

    Hi, I am trying to get my first generic method work. Here is what I want to do: Based on diffent type of users, we need to use different DLL. The DLL is COM object wrapper, which expose a function that can be called. The original code looks like the following: using System.Runtime.InteropServices; using AsynchWrapper1; using AsynchWrapper2; using AsynchWrapper3; .... namespace MyNameSpace { public class Unility { public static void CallAsynchWrapper(int clientType, int clientId) { if (clientType ==0) { clsAsynchWrapper1 asynchWrapper = new clsAsynchWrapper1(); asynchWrapper.XYZ(clientId); Marshal.ReleaseComObject(asynchWrapper); asynchWrapper = null; } else if (clientType ==1) { clsAsynchWrapper2 asynchWrapper = new clsAsynchWrapper2(); asynchWrapper.XYZ(clientId); Marshal.ReleaseComObject(asynchWrapper); asynchWrapper = null; } else { clsAsynchWrapper3 asynchWrapper = new clsAsynchWrapper3(); asynchWrapper.XYZ(clientId); Marshal.ReleaseComObject(asynchWrapper); asynchWrapper = null; } } } Where XYZ is the function that all 3 DLLs expose. I want to make a generic method by modifying the CallAsynchWrapper function doing the following: public static void CallAsynchWrapper(int clientType, int clientId) { if (clientType ==0) { clsAsynchWrapper1 asynchWrapper = new clsAsynchWrapper1(); genericCallAsynchWrapper(asynchWrapper, clientId); } else if (clientType ==1) { clsAsynchWrapper2 asynchWrapper = new clsAsynchWrapper2(); genericCallAsynchWrapper(asynchWrapper, clientId); } else { clsAsynchWrapper3 asynchWrapper = new clsAsynchWrapper3(); genericCallAsynchWrapper(asynchWrapper, clientId); } } and add a generic method: static void genericCallAsynchWrapper(T asynchWrapper, int clientId) { asynchWrapper.XYZ(clientId); Marshal.ReleaseComObject(asynchWrapper); asynchWrapper = default(T); } When I compile it, I got error message” T doesn’t have a definition of XYZ.”

    S 1 Reply Last reply
    0
    • V vicky457

      Hi, I am trying to get my first generic method work. Here is what I want to do: Based on diffent type of users, we need to use different DLL. The DLL is COM object wrapper, which expose a function that can be called. The original code looks like the following: using System.Runtime.InteropServices; using AsynchWrapper1; using AsynchWrapper2; using AsynchWrapper3; .... namespace MyNameSpace { public class Unility { public static void CallAsynchWrapper(int clientType, int clientId) { if (clientType ==0) { clsAsynchWrapper1 asynchWrapper = new clsAsynchWrapper1(); asynchWrapper.XYZ(clientId); Marshal.ReleaseComObject(asynchWrapper); asynchWrapper = null; } else if (clientType ==1) { clsAsynchWrapper2 asynchWrapper = new clsAsynchWrapper2(); asynchWrapper.XYZ(clientId); Marshal.ReleaseComObject(asynchWrapper); asynchWrapper = null; } else { clsAsynchWrapper3 asynchWrapper = new clsAsynchWrapper3(); asynchWrapper.XYZ(clientId); Marshal.ReleaseComObject(asynchWrapper); asynchWrapper = null; } } } Where XYZ is the function that all 3 DLLs expose. I want to make a generic method by modifying the CallAsynchWrapper function doing the following: public static void CallAsynchWrapper(int clientType, int clientId) { if (clientType ==0) { clsAsynchWrapper1 asynchWrapper = new clsAsynchWrapper1(); genericCallAsynchWrapper(asynchWrapper, clientId); } else if (clientType ==1) { clsAsynchWrapper2 asynchWrapper = new clsAsynchWrapper2(); genericCallAsynchWrapper(asynchWrapper, clientId); } else { clsAsynchWrapper3 asynchWrapper = new clsAsynchWrapper3(); genericCallAsynchWrapper(asynchWrapper, clientId); } } and add a generic method: static void genericCallAsynchWrapper(T asynchWrapper, int clientId) { asynchWrapper.XYZ(clientId); Marshal.ReleaseComObject(asynchWrapper); asynchWrapper = default(T); } When I compile it, I got error message” T doesn’t have a definition of XYZ.”

      S Offline
      S Offline
      S Senthil Kumar
      wrote on last edited by
      #2

      You need to add a constraint to your generic function to specify that the generic type you pass in will have a XYZ method. The best way to do that is to make all your clsAsynchWrapper1, 2 and 3 implement a common interface, with that interface having the XYZ function.

      interface IClsAsynchWrapper
      {
      void XYZ(int clientId);
      }

      class clsAsyncWrapper1 : IClsAsynchWrapper
      {...}

      static void genericCallAsynchWrapper<T>(T asynchWrapper, int clientId) where T: IClsAsyncWrapper
      {
      asynchWrapper.XYZ(clientId);
      }

      That said, I think polymorphism (through inheritance) would serve you better here than generics. How about having a base class, say ClsAsynchWrapper and have all the ClsAsyncWrapperX classes derive from it? You could then move the code in your generic method to the base class.

      Regards Senthil [MVP - Visual C#] _____________________________ My Blog | My Articles | My Flickr | WinMacro

      V 1 Reply Last reply
      0
      • S S Senthil Kumar

        You need to add a constraint to your generic function to specify that the generic type you pass in will have a XYZ method. The best way to do that is to make all your clsAsynchWrapper1, 2 and 3 implement a common interface, with that interface having the XYZ function.

        interface IClsAsynchWrapper
        {
        void XYZ(int clientId);
        }

        class clsAsyncWrapper1 : IClsAsynchWrapper
        {...}

        static void genericCallAsynchWrapper<T>(T asynchWrapper, int clientId) where T: IClsAsyncWrapper
        {
        asynchWrapper.XYZ(clientId);
        }

        That said, I think polymorphism (through inheritance) would serve you better here than generics. How about having a base class, say ClsAsynchWrapper and have all the ClsAsyncWrapperX classes derive from it? You could then move the code in your generic method to the base class.

        Regards Senthil [MVP - Visual C#] _____________________________ My Blog | My Articles | My Flickr | WinMacro

        V Offline
        V Offline
        vicky457
        wrote on last edited by
        #3

        Thank you so much! That's exactly what I need. I mean both interface and polymorphism. Thank you for enlightening me! I will try both ways and let you know how it goes. Thanks again!

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups