473,591 Members | 2,842 Online
Bytes | Software Development & Data Engineering Community
+ 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.Delet eByID(currentTy pe, id);
}
}

public class Order : Domain Object
{
}

If I call Order.DeleteByI D(), 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.GetC urrentMethod(). DeclaringType, but this
gives me "DomainObje ct". Is there any way I can get "Order" (the inheriting
class type)?
Many Thanks,
DrJazz
Aug 1 '06 #1
3 2474

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.Delet eByID(currentTy pe, id);
}
}

public class Order : Domain Object
{
}

If I call Order.DeleteByI D(), 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.GetC urrentMethod(). DeclaringType, but this
gives me "DomainObje ct". 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.DeleteByI d() compiles as
DomainObject.De leteById() so there is no way to distinguish at runtime.
You can check this w/ ildasm.

John

Aug 1 '06 #2
DrJazz <Dr****@communi ty.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.Delet eByID(currentTy pe, id);
}
}

public class Order : Domain Object
{
}

If I call Order.DeleteByI D(), 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.GetC urrentMethod(). DeclaringType, but this
gives me "DomainObje ct". 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
1931
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. As those classes methods are static, they are never associated with any instance, then not assoicated with any HttpRequest neither. Nevertheless in some case I ended up myself passing the Session object to my static methods to be able to get some...
6
2864
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 and the coresponding 'Id' from the lookup table is to be inserted into the field of the new record. I have two simple tables. "tblPerson" is the data table. The lookup
10
5060
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 within a static method (specifically a console's Main()). If it wasn't static, I'd just call LicenseManager.Validate(typeof(currentClass), this), but this isn't available when it's a static method. --
4
1399
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
3617
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 sending the Sandal type to the base class I can not instantiate a Sandal class from within Shoe. Public Class Shoe Public Shared Function Load() As Object return ?????
5
3601
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().
8
2624
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 the derived class but static methods cannot be overridden. If I can find out 'who i am' i can do what I need to do. thanks,
4
7663
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 longer ModelGenericBase<T>.test1() ? Andrus.
23
3192
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 an object.
0
7934
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
7870
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8362
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
7992
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
8225
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
3891
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2378
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
1465
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1199
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.