Want to validate a function and execute it
-
I have a typical problem. It is like I want to execute some API calls based on the Platform, if one API is not supported by the current platform it will throw an exception. I want to implement it like this:
[Platform(Platform.Win32NT)] [DllImport("somedll.dll")] public static extern int APIcall([In] int param1, float param2); public int CallTheAPI(int param1, float param2) { return APICall(parma1, param2); //If the current platform is not Win32NT it should throw an Error }
How should I implement Platform attribute and accomplish the operation? Please give some idea.Anindya Chatterjee -------------------------------------------------------- 1. Don't Visit ..[^] 2. But Watch ..
-
I have a typical problem. It is like I want to execute some API calls based on the Platform, if one API is not supported by the current platform it will throw an exception. I want to implement it like this:
[Platform(Platform.Win32NT)] [DllImport("somedll.dll")] public static extern int APIcall([In] int param1, float param2); public int CallTheAPI(int param1, float param2) { return APICall(parma1, param2); //If the current platform is not Win32NT it should throw an Error }
How should I implement Platform attribute and accomplish the operation? Please give some idea.Anindya Chatterjee -------------------------------------------------------- 1. Don't Visit ..[^] 2. But Watch ..
try this one.
[Platform(Platform.Win32NT)] [DllImport("somedll.dll")] public static extern int APIcall([In] int param1, float param2); public int CallTheAPI(int param1, float param2) { if (!System.Environment.OSVersion.Platform.ToString().Equals("Win32NT")) { throw new ApplicationException("Your current platform is not a Win32NT operating system."); } return APICall(parma1, param2); //If the current platform is not Win32NT it should }
-
try this one.
[Platform(Platform.Win32NT)] [DllImport("somedll.dll")] public static extern int APIcall([In] int param1, float param2); public int CallTheAPI(int param1, float param2) { if (!System.Environment.OSVersion.Platform.ToString().Equals("Win32NT")) { throw new ApplicationException("Your current platform is not a Win32NT operating system."); } return APICall(parma1, param2); //If the current platform is not Win32NT it should }
Basically , this is ok for one or two api calls. But I am here handling with more than 300 calls. So instead of putting the if conditions everywhere , just using attribute over each api signature would be lot easier. That is why i am posting this question to get it in a easier way.
Anindya Chatterjee -------------------------------------------------------- 1. Don't Visit ..[^] 2. But Watch ..
-
I have a typical problem. It is like I want to execute some API calls based on the Platform, if one API is not supported by the current platform it will throw an exception. I want to implement it like this:
[Platform(Platform.Win32NT)] [DllImport("somedll.dll")] public static extern int APIcall([In] int param1, float param2); public int CallTheAPI(int param1, float param2) { return APICall(parma1, param2); //If the current platform is not Win32NT it should throw an Error }
How should I implement Platform attribute and accomplish the operation? Please give some idea.Anindya Chatterjee -------------------------------------------------------- 1. Don't Visit ..[^] 2. But Watch ..
I guess you're looking for a generic method dispatch mechanism that validates the platform before executing calls. How about something like this.
enum Platform { XP, NT, Vista } class PlatformNameAttribute : Attribute { public PlatformNameAttribute(Platform platForm) { this.Platform = platForm; } public Platform Platform { get; set; } } class Program { \[PlatformName(Platform.NT)\] static void SomeAPICall() { Console.WriteLine("NT"); } \[PlatformName(Platform.XP)\] static void SomeOtherAPICall(int x) { Console.WriteLine("XP"); } static Platform GetCurrentPlatform() { return Platform.NT; } static void ExecuteAPICall(Delegate d, params object\[\] parameters) { var attributes = d.Method.GetCustomAttributes(typeof(PlatformNameAttribute), false); if (attributes != null && attributes.Count() > 0) { var platformAttribute = (PlatformNameAttribute)attributes\[0\]; if (platformAttribute.Platform != GetCurrentPlatform()) { throw new InvalidOperationException("Platform Type mismatch"); } d.DynamicInvoke(parameters); } } static void Main(string\[\] args) { ExecuteAPICall(new Action(SomeAPICall)); ExecuteAPICall(new Action<int>(SomeOtherAPICall), 2); }
You could do something more typesafe than DynamicInvoke, with Expression and lambdas, but that would require .NET 3.5, and I'm not sure you are running it.
Regards Senthil [MVP - Visual C#] _____________________________ My Home Page |My Blog | My Articles | My Flickr | WinMacro
-
I guess you're looking for a generic method dispatch mechanism that validates the platform before executing calls. How about something like this.
enum Platform { XP, NT, Vista } class PlatformNameAttribute : Attribute { public PlatformNameAttribute(Platform platForm) { this.Platform = platForm; } public Platform Platform { get; set; } } class Program { \[PlatformName(Platform.NT)\] static void SomeAPICall() { Console.WriteLine("NT"); } \[PlatformName(Platform.XP)\] static void SomeOtherAPICall(int x) { Console.WriteLine("XP"); } static Platform GetCurrentPlatform() { return Platform.NT; } static void ExecuteAPICall(Delegate d, params object\[\] parameters) { var attributes = d.Method.GetCustomAttributes(typeof(PlatformNameAttribute), false); if (attributes != null && attributes.Count() > 0) { var platformAttribute = (PlatformNameAttribute)attributes\[0\]; if (platformAttribute.Platform != GetCurrentPlatform()) { throw new InvalidOperationException("Platform Type mismatch"); } d.DynamicInvoke(parameters); } } static void Main(string\[\] args) { ExecuteAPICall(new Action(SomeAPICall)); ExecuteAPICall(new Action<int>(SomeOtherAPICall), 2); }
You could do something more typesafe than DynamicInvoke, with Expression and lambdas, but that would require .NET 3.5, and I'm not sure you are running it.
Regards Senthil [MVP - Visual C#] _____________________________ My Home Page |My Blog | My Articles | My Flickr | WinMacro
That's a cool solution and I already tried it. But the problem is when API calls uses parameters with ref / out keyword or it has a large number of parameters say 7 or something or have parameters with marshalas attributes you can't use either Action or Func. I am not sure in that case if this method dispatcher will work or not. For your information I am using .Net 3.5. Any kind of suggestion is highly required. Thanks.
Anindya Chatterjee -------------------------------------------------------- 1. Don't Visit ..[^] 2. But Watch ..