Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2014-01-23
5
Medium Priority
?
236 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 45

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 45

Accepted Solution

by:
AndyAinscow earned 1500 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

Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

Question has a verified solution.

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

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…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

877 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