Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / January 2008

Tip: Looking for answers? Try searching our database.

Is it possible to know within a function which object called it

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Avi - 31 Jan 2008 15:17 GMT
Hi experts,

I have a function that needs to be public.  However, I have a few objects in
my system that I would want to prevent them from calling this function.  Is
it possible to know within a function which object called it and exit when
it is one of the unwanted objects that called it?

Thanks,

Avi
not_a_commie - 31 Jan 2008 15:30 GMT
Look up the StackFrame class.
Ignacio Machin ( .NET/ C# MVP ) - 31 Jan 2008 16:39 GMT
Hi,

Can you make the method internal for example?
Maybe if you give more details about your design we can provide a better
approach.

Signature

Ignacio Machin
http://www.laceupsolutions.com
Mobile & warehouse Solutions.

> Hi experts,
>
[quoted text clipped - 6 lines]
>
> Avi
jehugaleahsa@gmail.com - 31 Jan 2008 17:57 GMT
> Hi experts,
>
[quoted text clipped - 6 lines]
>
> Avi

An approach to this problem would probably be to add a member named
likingly to "CanIRunThis" that takes the type of the class calling the
method. It simply returns true if the class type is valid.

NOTE! This is extremely dirty and shows that there may be a design
flaw in your code. You essentially desire different actions depending
on who calls the method. That sounds like polymorphism! Perhaps the
method should be a method inside the calling class, not the callee,
using virtual/abstract/interface, and have them provide the correct
action. Remember, methods are simply a matter of iterating and
processing data. You can seperate the data from the action and pass
the data into the abstract/virtual/interface method in the classes.

So, before you have this:

class MyBiasedClass
{
  // data members
  public void Process()
  {
     // impossible or dirty to check for caller.
     // execute if okay
  }
}

class One
{
  MyBiasedClass mbc; // initialized somehow
  void Process()
  {
     mbc.Process(); // this may do nothing at all
  }
}

// etc.

What if you did this:

class One : IBiasedProcessor
{
   public BiasedProcess(MyBiasedData data)
   {
      // do whatever you need with the data
   }
}

class Two : IBiasedProcessor
{
  public BiasedProcess(MyBiasedData data)
  {
      // do nothing
  }
}

// etc.

The question then becomes, who is responsible for executing the
method. When you start doing things like you're asking about, your
code becomes stagnant and unchanging. If you ever add anything to your
code, you would have to remember to modify the class to accept the new
type. This is a nightmare!

Finally, a slightly easier approach is to create an interface that
acts as a marker. Call is ICanBeProcessed and leave it empty.

interface ICanBeProcessed {}

Any class that supports being processed should implement this
interface.

Then BiasedProcessor.Process() looks like this:

public Process(object myCaller) // object is as simple as it gets
{
   if (myCaller is ICanBeProcessed)
   {
       // execute code
   }
}

Again, that's dirty because you have to use reflection and broaden
your parameter to object. You can make all calling classes
ICanBeProcessed if it supports and CanIBeProcessed method or property
that returns true or false. Then you can get away from reflection and
get a compile time check that all callers are of type ICanBeProcessed.
The problem then is that you are limiting yourself to an interface,
and any type-specific information is hidden unless you do an explicit
cast. In that case, you can either provide more interface properties
or methods to get that data, or you can go to an abstract class.

It is really a matter of how far you are willing to go to make good
code. Some times the best way to do this is to find a way to make the
two classes communicate by passing the caller to the callee as an
abstract instance and calling the needed methods, back and forth.

Try it or try to try it!
Ignacio Machin ( .NET/ C# MVP ) - 31 Jan 2008 20:41 GMT
Hi,

That is also my opinion, that there is an error in the design.

Signature

Ignacio Machin
http://www.laceupsolutions.com
Mobile & warehouse Solutions.

On Jan 31, 8:17 am, "Avi" <remember...@yahoo.com> wrote:
> Hi experts,
>
[quoted text clipped - 7 lines]
>
> Avi

An approach to this problem would probably be to add a member named
likingly to "CanIRunThis" that takes the type of the class calling the
method. It simply returns true if the class type is valid.

NOTE! This is extremely dirty and shows that there may be a design
flaw in your code. You essentially desire different actions depending
on who calls the method. That sounds like polymorphism! Perhaps the
method should be a method inside the calling class, not the callee,
using virtual/abstract/interface, and have them provide the correct
action. Remember, methods are simply a matter of iterating and
processing data. You can seperate the data from the action and pass
the data into the abstract/virtual/interface method in the classes.

So, before you have this:

class MyBiasedClass
{
  // data members
  public void Process()
  {
     // impossible or dirty to check for caller.
     // execute if okay
  }
}

class One
{
  MyBiasedClass mbc; // initialized somehow
  void Process()
  {
     mbc.Process(); // this may do nothing at all
  }
}

// etc.

What if you did this:

class One : IBiasedProcessor
{
   public BiasedProcess(MyBiasedData data)
   {
      // do whatever you need with the data
   }
}

class Two : IBiasedProcessor
{
  public BiasedProcess(MyBiasedData data)
  {
      // do nothing
  }
}

// etc.

The question then becomes, who is responsible for executing the
method. When you start doing things like you're asking about, your
code becomes stagnant and unchanging. If you ever add anything to your
code, you would have to remember to modify the class to accept the new
type. This is a nightmare!

Finally, a slightly easier approach is to create an interface that
acts as a marker. Call is ICanBeProcessed and leave it empty.

interface ICanBeProcessed {}

Any class that supports being processed should implement this
interface.

Then BiasedProcessor.Process() looks like this:

public Process(object myCaller) // object is as simple as it gets
{
   if (myCaller is ICanBeProcessed)
   {
       // execute code
   }
}

Again, that's dirty because you have to use reflection and broaden
your parameter to object. You can make all calling classes
ICanBeProcessed if it supports and CanIBeProcessed method or property
that returns true or false. Then you can get away from reflection and
get a compile time check that all callers are of type ICanBeProcessed.
The problem then is that you are limiting yourself to an interface,
and any type-specific information is hidden unless you do an explicit
cast. In that case, you can either provide more interface properties
or methods to get that data, or you can go to an abstract class.

It is really a matter of how far you are willing to go to make good
code. Some times the best way to do this is to find a way to make the
two classes communicate by passing the caller to the callee as an
abstract instance and calling the needed methods, back and forth.

Try it or try to try it!

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.