2

There are some classes (of the .NET framework 3.5) that contain some methods that are supported in .NET Compact Framework, and some methods that are not supported. There are also some classes that does not exists for the .NET Compact Framework.

For example for the System.IO.File class, the File.Create function is supported by .NET Compact Framework, but the File.Encrypt function is not.

Another example: the System.IO.File class is supported by .NET Compact Framework, but the System.Diagnostic.StackTrace is not.

I need to tell to the compiler something like this:

#ifdef COMPACT_FRAMEWORK   // I'm compiling this from a smart device project

MyEncryptMethod("filename");

#else // I'm compiling this from a desktop project

File.Encrypt("filename");

#endif

How can I do?
(The specific version is Windows Mobile 6.1 Professional).

Alvin Wong
  • 12,210
  • 5
  • 51
  • 77
Nick
  • 10,309
  • 21
  • 97
  • 201
  • Well, what's wrong with the code you provided? – Kevin Gosse Nov 06 '12 at 12:38
  • Well, read MSDN carefully. I believe you could use reflection to check, but in case your program *must* work you always need to check the documentation when programming it and provide alternative methods. – Alvin Wong Nov 06 '12 at 12:45
  • @KooKiz COMPACT_FRAMEWORK is a word I invented. – Nick Nov 06 '12 at 12:45
  • http://social.msdn.microsoft.com/Forums/en-US/netfxcompact/thread/788e3ad0-43be-4755-93f0-3ac7dd2aea51/ – Alvin Wong Nov 06 '12 at 12:46
  • 1
    You can just define a build configuration that you'll use when building for the compact framework. In this build configuration, define the COMPACT_FRAMEWORK conditional compilation symbol. – Kevin Gosse Nov 06 '12 at 12:47
  • @KooKiz yes, you're right. I had not thought. – Nick Nov 06 '12 at 12:49
  • You may also start a SmartDevice project and run that on desktop windows too except you are using some CompactFramework-only APIs/Assemblies. - Another approach would be handling MissingMethod exceptions in code lines that use Full Framework code lines. – josef Nov 07 '12 at 08:42

3 Answers3

3

Just to add, since you are showing windows-mobile and windows-mobile-6, you should change your #define constraint to PocketPC instead of COMPACT_FRAMEWORK.

#ifdef PocketPC   // PocketPC is what the WM SDK uses

MyEncryptMethod("filename");

#else // I'm compiling this from a desktop project

File.Encrypt("filename");

#endif

Update:

Nick: What yms said. :) When building a project using one of the Smart Device projects, Visual Studio automatically add the conditional compilation symbol PocketPC to the project.

From within VS2008's Main Menu, click Project and select your project's Properties at the bottom.

On your project's Properties page, go to the Build tab, and there you will see where PocketPC is already defined for you.

  • Can you elaborate better? Why Pocket PC instead of Compact Framework? What's the difference? – Nick Nov 07 '12 at 17:01
  • 1
    @Nick Visual Studio adds the symbol PocketPC by default to WinMo projects – yms Nov 07 '12 at 18:45
2

The code you provided is good, you just have to define the COMPACT_FRAMEWORK compilation symbol.

First, define a build configuration that you'll use when building your assembly for the compact framework. Then, in this build configuration, just define the COMPACT_FRAMEWORK conditional compilation symbol.

Conditional compilation symbols are defined in the Build tab of the project properties.

Kevin Gosse
  • 38,392
  • 3
  • 78
  • 94
0

Here is some code the looks for a Method inside a class:

    public static bool execCmd(string sFunc, string sArg, ref string sResponse)
    {
        bool bRet = true;
        try
        {
            // Instantiate this class
            myCommands cmbn = new myCommands(sFunc, sArg);

            // Get the desired method by name: DisplayName
            //MethodInfo methodInfo = typeof(CallMethodByName).GetMethod("DisplayName");
            MethodInfo methodInfo = typeof(myCommands).GetMethod(sFunc);

            // Use the instance to call the method without arguments
            methodInfo.Invoke(cmbn, null);
            sResponse = cmbn.response;
        }
        catch (Exception ex)
        {
            System.Diagnostics.Debug.WriteLine("Exception in execCmd for '" + sFunc + "' and '" + sArg + "' " + ex.Message); 
            bRet = false; 
        }
        return bRet;
    }

You have to change myCommands to the class you are searching and sFunc has to be set to the Method you are looking for. With that code you can check if a method exists in a class.

~josef

josef
  • 5,951
  • 1
  • 13
  • 24