473,398 Members | 2,389 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Calling Class Type

acg
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
Nov 15 '05 #1
3 3908
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" <an*******@discussions.microsoft.com> wrote in message
news:80**********************************@microsof t.com...
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

Nov 15 '05 #2

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
Nov 15 '05 #3
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" <an*******@discussions.microsoft.com> wrote in message
news:80**********************************@microsof t.com...
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

Nov 15 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Chris | last post by:
Hi I have a scenario where I've created another AppDomain to dynamically load a DLL(s) into. In this newly loaded DLL I want to call a static method on a class. The problem arise is that I have...
6
by: Eric Lilja | last post by:
Hello, I have a std::vector storing pointers to an abstract base class OptionBase. OptionBase is not templated but I am deriving a template class called Option from OptionBase. The class Option has...
5
by: Nick Flandry | last post by:
I'm running into an Invalid Cast Exception on an ASP.NET application that runs fine in my development environment (Win2K server running IIS 5) and a test environment (also Win2K server running IIS...
6
by: Amjad | last post by:
Hi, I want to make a project that calls and executes a function (VB code) made in a seperate file in the Application Folder. I know I can create the function in my project and call it internally,...
7
by: Christian Wilhelm | last post by:
Hi! I'm trying to call a Java WebService out of a .net Client. There are two Methods, one Method requires one Parameter of type Parameter, the other Method requires one Parameter of type...
3
by: Jerome Cohen | last post by:
AI am trying to call a third-party web service. this service expects an XML fragment that contains the request plus other parameter. adding the web reference created the syntax below(reference.vb)....
12
by: scottt | last post by:
hi, I am having a little problem passing in reference of my calling class (in my ..exe)into a DLL. Both programs are C# and what I am trying to do is pass a reference to my one class into a DLL...
10
by: Finger.Octopus | last post by:
Hello, I have been trying to call the super constructor from my derived class but its not working as expected. See the code: class HTMLMain: def __init__(self): self.text = "<HTML><BODY>";...
15
by: =?Utf-8?B?VG9tIENvcmNvcmFu?= | last post by:
I've been led to believe by several articles, particularly Eric Gunnerson's C# Calling Code Dynamically, that calling a method dynamically through Reflection was much slower than through a...
7
by: =?Utf-8?B?UVNJRGV2ZWxvcGVy?= | last post by:
I have a C# logging assembly with a static constructor and methods that is called from another C# Assembly that is used as a COM interface for a VB6 Application. Ideally I need to build a file...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.