473,473 Members | 2,050 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Get Current Type Within Inherited Static Method

Consider the following (which may be bad architecturally speaking, but please
humor me):

public class DomainObject
{
public static void DeleteByID(int id)
{
// Type currentType = ????;
DataLayer.DeleteByID(currentType, id);
}
}

public class Order : Domain Object
{
}

If I call Order.DeleteByID(), how can I tell that it was the Order class
that was called? Because it's a static method, I can't use "this". If I use
Reflection, I can use MethodBase.GetCurrentMethod().DeclaringType, but this
gives me "DomainObject". Is there any way I can get "Order" (the inheriting
class type)?
Many Thanks,
DrJazz
Aug 1 '06 #1
3 2464

DrJazz wrote:
Consider the following (which may be bad architecturally speaking, but please
humor me):

public class DomainObject
{
public static void DeleteByID(int id)
{
// Type currentType = ????;
DataLayer.DeleteByID(currentType, id);
}
}

public class Order : Domain Object
{
}

If I call Order.DeleteByID(), how can I tell that it was the Order class
that was called? Because it's a static method, I can't use "this". If I use
Reflection, I can use MethodBase.GetCurrentMethod().DeclaringType, but this
gives me "DomainObject". Is there any way I can get "Order" (the inheriting
class type)?
Many Thanks,
DrJazz
Hi DrJazz,
I don't think this is possible. Calling Order.DeleteById() compiles as
DomainObject.DeleteById() so there is no way to distinguish at runtime.
You can check this w/ ildasm.

John

Aug 1 '06 #2
DrJazz <Dr****@community.nospamwrote:
Consider the following (which may be bad architecturally speaking, but please
humor me):

public class DomainObject
{
public static void DeleteByID(int id)
{
// Type currentType = ????;
DataLayer.DeleteByID(currentType, id);
}
}

public class Order : Domain Object
{
}

If I call Order.DeleteByID(), how can I tell that it was the Order class
that was called? Because it's a static method, I can't use "this". If I use
Reflection, I can use MethodBase.GetCurrentMethod().DeclaringType, but this
gives me "DomainObject". Is there any way I can get "Order" (the inheriting
class type)?
There's no inheritance of static methods - there's only one definition.
There is a kind of inheritance of class namespace, but it compiles to
reference the one and only static method, on whatever class it was
defined.

-- Barry

--
http://barrkel.blogspot.com/
Aug 1 '06 #3
That's what I thought. Thanks for the quick replies, gentlemen. I appreciate
your time.
Cheers,
DrJazz
Aug 1 '06 #4

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

Similar topics

2
by: Francois Malgreve | last post by:
hello guys, I have some helper class in my ASP.NET pplication who basically contains static methods. I used them as helper methods to do small jobs that I can use at many places in my code. ...
6
by: dbuchanan | last post by:
VS2005 I've been reading all the help I can on the topic (MSDN, other) but I can't make sense of this. Desired behavior; The user is to choose from the displayed list of the databound combobox...
10
by: AC [MVP MCMS] | last post by:
How do you get the current instance of an executing assembly from within a static method? I'm trying to use the LicenseManager.Validate(type,instance) method and having trouble calling it from...
4
by: Dan | last post by:
I have class B and C which inherit from class A. I have a static method: A* aRequest(unsigned char *byte_buffer, size_t length) { A *foo; if(something == true) { foo = new B;
6
by: roland.bali | last post by:
Hi, Here is the basic setup, my base class is Shoe which has a child class called Sandal. I would like to create objects by calling Sandal.Load. But without overloading Load in Sandal and...
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...
8
by: John Mott | last post by:
Hi all, Is there a way to get the name of the current class in a static method? I have a base class that contains static methods and that class is inherited. I'd like to change the behavior in...
4
by: Andrus | last post by:
I tried to use type parameter to call static method but got compile error 'T' is a 'type parameter', which is not valid in the given context. Why this is not allowed ? Should I really use a lot...
23
by: dhtmlkitchen | last post by:
JSON We all know what it is. In ECMAScript 4, there's a JSON proposal: Object.prototype.toJSONString String.prototype.parseJSON The current proposal, String.prototype.parseJSON, returns...
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
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...
1
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...
0
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,...
0
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...
0
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 ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
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...

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.