473,769 Members | 7,320 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 1243
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
52758
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
1479
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
2678
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
3199
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
1761
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
3609
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
1775
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
3396
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
17949
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
9589
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
10216
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...
1
9997
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
8873
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...
0
6675
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
5309
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
3965
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
2
3565
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2815
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.