473,320 Members | 1,722 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,320 software developers and data experts.

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.MyMethod() // calling the static method defined in the
abstract class
}
}
Regards,
Bob Rock

Dec 28 '06 #1
8 1227
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.MyMethod() // calling the static method defined in the
abstract class
}
}
I don't think so.

Because you are really calling MyAbstract.MyMethod ...

You can get MyAbstract with:

MethodInfo.GetCurrentMethod().DeclaringType.FullNa me

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********************@hotmail.com.nospamwrote in message
news:O5**************@TK2MSFTNGP06.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.MyMethod() // 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.MyMethod ...

You can get MyAbstract with:

MethodInfo.GetCurrentMethod().DeclaringType.FullNa me

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**************@TK2MSFTNGP06.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********************@hotmail.com.nospamwrote in message
news:O5**************@TK2MSFTNGP06.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.MyMethod() // calling the static method defined in the
abstract class
}
}
Regards,
Bob Rock


Dec 29 '06 #7
public class MyConcrete : MyAbstact
{
public MyOtherMethod()
{
MyConcrete.MyMethod() // 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********************@hotmail.com.nospamwrote in message
news:Oj**************@TK2MSFTNGP06.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**************@TK2MSFTNGP06.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********************@hotmail.com.nospamwrote in message
news:O5**************@TK2MSFTNGP06.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.MyMethod() // 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
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...
5
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
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...
3
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...
10
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...
5
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...
3
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)....
0
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...
5
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.