Question

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

Was it helpful?

Solution

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.

OTHER TIPS

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.

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top