how to call DLL function in generic method?
-
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.”
-
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.”
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
-
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