Get Current Type Within Inherited Static Method

G

Guest

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
 
J

John Duval

DrJazz said:
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
 
B

Barry Kelly

DrJazz said:
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
 
G

Guest

That's what I thought. Thanks for the quick replies, gentlemen. I appreciate
your time.


Cheers,
DrJazz
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top