Solved

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

Posted on 2014-01-23
5
234 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

623 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