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. Want to validate a function and execute it

Want to validate a function and execute it

Scheduled Pinned Locked Moved C#
helpcomjsonquestion
5 Posts 3 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.
  • A Offline
    A Offline
    Anindya Chatterjee
    wrote on last edited by
    #1

    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 ..

    T S 2 Replies Last reply
    0
    • A Anindya Chatterjee

      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 ..

      T Offline
      T Offline
      Tuwing Sabado
      wrote on last edited by
      #2

      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 }

      A 1 Reply Last reply
      0
      • T Tuwing Sabado

        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 }

        A Offline
        A Offline
        Anindya Chatterjee
        wrote on last edited by
        #3

        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 ..

        1 Reply Last reply
        0
        • A Anindya Chatterjee

          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 ..

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

          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

          A 1 Reply Last reply
          0
          • S S Senthil Kumar

            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

            A Offline
            A Offline
            Anindya Chatterjee
            wrote on last edited by
            #5

            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 ..

            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