Solved

How to prevent calling of public method by caller application type compile time?

Posted on 2014-01-23
5
224 Views
Last Modified: 2014-01-29
Hi,

i have a common library which contains several public methods. some methods include session and application objects which will be available in web projects. but because of publicitiy of those methods, a windows forms application can call those methods too. it will produce run time error because of unavailability of session objects.

what would be the best way to prevent those methods usage in compile time from windows applications? i am seeking for an easy implementation, complex solutions will not be useful for me.

thx.
0
Comment
Question by:damarbt
5 Comments
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 39802994
>>what would be the best way to prevent those methods usage in compile time from windows applications?

That implies two versions, one for web and one for windows apps.  (For one version then the answer is simple - you can't do that).
You can define a symbol and then at compile time activate or deactivate code as appropriate.

eg.
#if WINAPPS
#else
  public void Foo()
  {
  }
#endif

so at compile time the version where WINAPPS is defined will not have the function Foo available.
0
 
LVL 12

Expert Comment

by:jmcmunn
ID: 39803071
To me it seems as though you should actually have two assemblies, one for the web and one for the desktop apps.  If they share code that you (actually) want shared then make a third assembly for them both to use.

There is no way to have the code in there (and compiled) and have public methods not callable.  However as stated above if you are recompiling with a flag that is defined in your desktop, you can surround the code as shown.

And of course, any method should do its job and handle exceptions and bad arguments/variables before using them.  So, if you need to know if session is there then test for it before using it.  ASSERT that what you need is valid...though at that point the method would likely be pointless I assume, and a desktop programmer should not be calling it.
0
 
LVL 40
ID: 39804606
If these methods need to be called only internally by the Library, declare them as internal. They will work as public in the library, but private outside of it.

If you need to use that Library with some of your own applications, then declare them as Friend assemblies, and they will be able to get access to the internal methods. See http://msdn.microsoft.com/en-us/library/0tke9fxk.aspx
0
 

Author Comment

by:damarbt
ID: 39806555
seperate libraries is possible but not a great way i guess.

what about checking HttpContext.Current == null condition. For nonweb applications HttpContext.Current will be null but i am not sure if it is trustable or not
0
 
LVL 44

Accepted Solution

by:
AndyAinscow earned 500 total points
ID: 39806610
As I said earlier if you wanted to implement at compile time then you do need two versions.
With one version you can never prevent a windows app calling a web specific component.

One possibility which would work is to have a singleton.  That contains an enumerated value.  This has three values: not initialised (the default), web and windows.  You set if this value is web or windows yourself.  Any call into the function and that finds the value in the singleton is not initialised then you force the function to crash with a warning to the developer that it must be initialised before the library can be used.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now