Monday, March 11, 2013

Conditional using statement in C# debug versus Release

Hi Folks,

I'm writing this blog post to share how to write conditional using statements in C#. The meaning of this is how to use using statements in Debug mode versus Release mode in reference to different dlls. This is needed when you are using any testing/sandbox DLLs that use any non production environment and you want your code to use other reference when you build a Release for your application in reference to production dlls.

The below code is using In-App Purchase library for Windows Phone 8.

C# Code:

#if DEBUG
using MockIAPLib;
using Store = MockIAPLib;
#else
using Windows.ApplicationModel.Store;
#endif
Notice: you give the same alias name for the testing Libaray namespace as the one that will be used in production so you don't need to re-write your code.

Then, you can use any class for the referenced namespace:

ProductListing p = new ProductListing();


Tip: You should select Release before your deploy your application to products so the application will refer to the production assemblies and not the ones are used in the debug mode.


Hope this tip helps!

 

No comments: