Calling Class Type

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

If you have a class with a public method, and another class which will want to call this method, is there a way to determine the type of the calling class within the method being called?

For I want to gaurantee only certain other classes/object types can call a specific method in given class. Any advice would be appreciated and if there is anything that can be done to use the frameworks inherent permissioning.

Regards,
acg
 
You can probably determine the calling class by examining the stack,
although I wouldn't recommend it.

You can also follow the model commonly seen in handling events, in
which the sender is sent as a parameter. Your class' function would look
like myClass.DoWhatever(object sender, ...)

Then you could evaluate sender.GetType() and do whatever you need.

Your best bet, however, is to design your classes with member visibility
in mind (private, protected, public), and ask yourself why certain classes
should be allowed to call methods and not others, and design with
inheritance, with these ideas in mind.

Erik

acg said:
If you have a class with a public method, and another class which will
want to call this method, is there a way to determine the type of the
calling class within the method being called?
For I want to gaurantee only certain other classes/object types can call a
specific method in given class. Any advice would be appreciated and if
there is anything that can be done to use the frameworks inherent
permissioning.
 
Define an Abstract class or an interfaces, Derive your objects family through them. In the callee check the caller using the "is" keyworld..

interface IBase
int Add()


class Derived : IBase
public int Add ()
return 0



class Class

[STAThread
static void Main(string[] args)
Derived d = new Derived ()
if (d is IBase
Console.WriteLine ( "d is IBase type" )
els
Console.WriteLine ( "Sorry, i dont like this object" )
Console.Read ()



Rafat Saros
----- acg wrote: ----

If you have a class with a public method, and another class which will want to call this method, is there a way to determine the type of the calling class within the method being called

For I want to gaurantee only certain other classes/object types can call a specific method in given class. Any advice would be appreciated and if there is anything that can be done to use the frameworks inherent permissioning

Regards
acg
 
The other reply to this has some good information. In general, you may want
to look at the design first and see why you're doing this and if there's a
better way to handle it.

But I have been in a position where this was the best solution. You can use
StackTrace.GetFrame() and StackFrame.GetMethod() to go up the stack to the
calling method. But you really should investigate the previous poster's
advice.

Pete

acg said:
If you have a class with a public method, and another class which will
want to call this method, is there a way to determine the type of the
calling class within the method being called?
For I want to gaurantee only certain other classes/object types can call a
specific method in given class. Any advice would be appreciated and if
there is anything that can be done to use the frameworks inherent
permissioning.
 
Back
Top