473,888 Members | 1,512 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Retrieving from inside a static method the type of the containing class???

Hello,

is there a way of retrieving the type of the class containing a static
method from the method itself when it gets called?
I have this situation: an abstact class that defines a static method, a
class that derives from the abstract class and I need

to retrieve the type of the concrete class when calling the static method.

public abstact class MyAbstact
{
public static MyMethod()
{
// is there a way of getting the type of the class (i.e. MyConcrete) from
inside here???
}
}

public class MyConcrete : MyAbstact
{
public MyOtherMethod()
{
MyConcrete.MyMe thod() // calling the static method defined in the
abstract class
}
}
Regards,
Bob Rock

Dec 28 '06 #1
8 1250
maybe

typeof(this)

??
Dec 29 '06 #2
XOR wrote:
maybe

typeof(this)
He said static method ...

Arne
Dec 29 '06 #3
Bob Rock wrote:
is there a way of retrieving the type of the class containing a static
method from the method itself when it gets called?
I have this situation: an abstact class that defines a static method, a
class that derives from the abstract class and I need

to retrieve the type of the concrete class when calling the static method.

public abstact class MyAbstact
{
public static MyMethod()
{
// is there a way of getting the type of the class (i.e. MyConcrete) from
inside here???
}
}

public class MyConcrete : MyAbstact
{
public MyOtherMethod()
{
MyConcrete.MyMe thod() // calling the static method defined in the
abstract class
}
}
I don't think so.

Because you are really calling MyAbstract.MyMe thod ...

You can get MyAbstract with:

MethodInfo.GetC urrentMethod(). DeclaringType.F ullName

But that is rather pointless.

Arne
Dec 29 '06 #4
Hi Bob,

No. For that requirement you should either make MyMethod an instance
method, accept a Type parameter or make it generic in the 2.0 framework if
that would add any value.

--
Dave Sexton
http://davesexton.com/blog

"Bob Rock" <ye************ ********@hotmai l.com.nospamwro te in message
news:O5******** ******@TK2MSFTN GP06.phx.gbl...
Hello,

is there a way of retrieving the type of the class containing a static
method from the method itself when it gets called?
I have this situation: an abstact class that defines a static method, a
class that derives from the abstract class and I need

to retrieve the type of the concrete class when calling the static method.

public abstact class MyAbstact
{
public static MyMethod()
{
// is there a way of getting the type of the class (i.e. MyConcrete)
from inside here???
}
}

public class MyConcrete : MyAbstact
{
public MyOtherMethod()
{
MyConcrete.MyMe thod() // calling the static method defined in the
abstract class
}
}
Regards,
Bob Rock

Dec 29 '06 #5
Yes, that I already tried and unfortunately the abstract class and not the
concrete one is returned. Even navigating the call stack does not help.
Again the abstract type and not the concrete one is returned.

Bob Rock
>
I don't think so.

Because you are really calling MyAbstract.MyMe thod ...

You can get MyAbstract with:

MethodInfo.GetC urrentMethod(). DeclaringType.F ullName

But that is rather pointless.

Arne

Dec 29 '06 #6
Yes, that is something I was trying to avoid ... it would dirty my otherwise
perfect design :-(

Bob Rock
"Dave Sexton" <dave@jwa[remove.this]online.comwrote in message
news:uS******** ******@TK2MSFTN GP06.phx.gbl...
Hi Bob,

No. For that requirement you should either make MyMethod an instance
method, accept a Type parameter or make it generic in the 2.0 framework if
that would add any value.

--
Dave Sexton
http://davesexton.com/blog

"Bob Rock" <ye************ ********@hotmai l.com.nospamwro te in message
news:O5******** ******@TK2MSFTN GP06.phx.gbl...
>Hello,

is there a way of retrieving the type of the class containing a static
method from the method itself when it gets called?
I have this situation: an abstact class that defines a static method, a
class that derives from the abstract class and I need

to retrieve the type of the concrete class when calling the static
method.

public abstact class MyAbstact
{
public static MyMethod()
{
// is there a way of getting the type of the class (i.e. MyConcrete)
from inside here???
}
}

public class MyConcrete : MyAbstact
{
public MyOtherMethod()
{
MyConcrete.MyMe thod() // calling the static method defined in the
abstract class
}
}
Regards,
Bob Rock


Dec 29 '06 #7
public class MyConcrete : MyAbstact
{
public MyOtherMethod()
{
MyConcrete.MyMe thod() // calling the static method defined in the
abstract class
The use of MyConcrete doesn't survive the compiler's name resolution pass.
Check the MSIL.
}
}

Dec 29 '06 #8
Hi Bob,

I'm not sure what it is that you're doing, but if your method relies on the
Type of a derived class then your design probably isn't perfect to begin
with ;)

--
Dave Sexton
http://davesexton.com/blog

"Bob Rock" <ye************ ********@hotmai l.com.nospamwro te in message
news:Oj******** ******@TK2MSFTN GP06.phx.gbl...
Yes, that is something I was trying to avoid ... it would dirty my
otherwise perfect design :-(

Bob Rock
"Dave Sexton" <dave@jwa[remove.this]online.comwrote in message
news:uS******** ******@TK2MSFTN GP06.phx.gbl...
>Hi Bob,

No. For that requirement you should either make MyMethod an instance
method, accept a Type parameter or make it generic in the 2.0 framework
if that would add any value.

--
Dave Sexton
http://davesexton.com/blog

"Bob Rock" <ye************ ********@hotmai l.com.nospamwro te in message
news:O5******* *******@TK2MSFT NGP06.phx.gbl.. .
>>Hello,

is there a way of retrieving the type of the class containing a static
method from the method itself when it gets called?
I have this situation: an abstact class that defines a static method, a
class that derives from the abstract class and I need

to retrieve the type of the concrete class when calling the static
method.

public abstact class MyAbstact
{
public static MyMethod()
{
// is there a way of getting the type of the class (i.e. MyConcrete)
from inside here???
}
}

public class MyConcrete : MyAbstact
{
public MyOtherMethod()
{
MyConcrete.MyMe thod() // calling the static method defined in the
abstract class
}
}
Regards,
Bob Rock



Dec 29 '06 #9

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

Similar topics

8
52767
by: Steven Livingstone | last post by:
Anyone able to explain to me why you cannot define an interface that can then be implemented using static methods? I understand the C# CLS states this, but just interested in the reasons behind it. thanks, Steven
5
1485
by: Ren? Paw Christensen | last post by:
Hi. Considering the following code, I want this call: Something.Method(); To return "Something". public class BaseClass { public static string Method() {
9
2690
by: thomson | last post by:
Hi all, Would you please explain me where will be the heap stored if it is declared inside the Class, As class is a reference type, so it gets stored on the heap, but struct is a value type-stored on the stack Regards thomson
3
3207
by: Mark R. Dawson | last post by:
Hi all, I am trying to get custom attributes from a property. I can do this if I pass in the name of the property i.e. "Name" to the reflection methods, but if I pass in set_Name which is what the set piece of the Name property gets compiled to, which I am getting from the stack trace, then the attributes are not returned. For example, Class Person has a property called "Name" which has a custom attribute decorating it. Inside the set...
10
1767
by: John A Grandy | last post by:
Say I have Class1 which contains static Class2 var1 = new Class2(); Is Class2 constructor code only executed if var1 is referenced in the code-execution path ? Or is Class2 constructor code executed on load of any class that references Class1 ?
5
3613
by: Eliseu Rodrigues | last post by:
Hi I would like to have a static method on a base class that executes some action (for example retrieves the row count) on a table whose name is the same of the inherited class name. For example: Assume that my base class has a static method named GetRowCount().
3
1778
by: William Stacey [C# MVP] | last post by:
It would be handy to be able to ref "this" from inside an AM such as: (string s) { Console.Writeline(s); DoSomething(this); } So treating am like a method of a class (which it is). Currently we have no context to know so you have to pass that as state which seems kinda
0
3400
bmallett
by: bmallett | last post by:
First off, i would like to thank everyone for any and all help with this. That being said, I am having a problem retrieving/posting my dynamic form data. I have a form that has multiple options within options. I have everything being dynamically named from the previously dynamically named element. (I hope this makes sense.) I am not able to retrieve any of the dynamically created values. I can view them on the source page but can't pull them...
5
17953
by: Jeff Dege | last post by:
In a non-static method, I can obtain the Type of the class containing that method with "this.GetType()". In a static method, I cannot, of course. There is no "this". I have a method in another class that needs to be passed a Type object associated with a class. I'm calling that method from a static method, and want to pass the Type object associated with the class that contains that static method.
0
9961
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10778
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...
0
10439
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9597
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
7990
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
5824
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...
0
6014
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4642
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
3
3252
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.