473,608 Members | 2,457 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3924
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.DoWhate ver(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*******@disc ussions.microso ft.com> wrote in message
news:80******** *************** ***********@mic rosoft.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.WriteLi ne ( "d is IBase type" )
els
Console.WriteLi ne ( "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.GetF rame() and StackFrame.GetM ethod() to go up the stack to the
calling method. But you really should investigate the previous poster's
advice.

Pete

"acg" <an*******@disc ussions.microso ft.com> wrote in message
news:80******** *************** ***********@mic rosoft.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
15356
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 the same class/static method definition statictly linked to my EXE and when I call InvokeMember(...), even though I got the Type from the new AppDomain, it calls the static method that I am staticly linked to and not the static method in the dynamicly...
6
1573
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 a public member function called get_value() that is not present in the class OptionBase. My problem is that when calling get_value() I have to cast to Option<some_type>* and I was wondering if I can somehow remove the cast so I dont have to...
5
3413
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 5), but fails on IIS 6 running on a Win2003 server. The web uses Pages derived from a custom class I wrote (which itself derives from Page) to provide some common functionality. The Page_Load handler the failing webpage starts out like this: ...
6
1695
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, but I want to put the function's code in an external file, so that future updates to the function will require a replacing the function file instead of re-installing the whole project. Can you give me ideas on how I can make small updates to my...
7
4972
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 Parameter. I can call the first Method without Problems, the Parameter can be deserialized by the WebService. But if I want to call the second Method and give it an Array of Parameters, then the following exception is thrown by the WebService:...
3
5055
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). I changed the data type for the structure that contains the XML data from the default "String" to "xml.xmldocument" to enable easy filling of the data. my client code creates an XML document class, fills the data using standard xml dom...
12
3012
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 function. When I try and compile the DLL I get "The type or namespace name "MyForm" could not be found. I think I have to reference the class but since the DLL needs to be built before the EXE it looks like I have a chicken and egg type problem....
10
13286
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>"; print(self.text); def __del__(self): self.text = "</BODY></HTML>"; print(self.text);
15
8187
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 Delegate. My testing showed that actually it was six times faster: 0.5 seconds for 100,000 iterations versus 3.1 seconds. Can anyone explain why? Something in the way I coded it? I'd appreciate any insights. Here's the code (in a Windows Form...
7
2673
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 name based on the name of the VB6 application. A second choice would be a file name based on the # COM interface assembly. I have tried calling Assembly.GetCallingAssembly() but this fails when I use the VB6 client. Is there a way to get this...
0
8495
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8470
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8145
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6815
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6011
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5475
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3960
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2474
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1589
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.