static virtual/override

T

Tony Maresca

Don't know if there are any Delphi/Object Pascal converts
here, but being one, I sorely miss the feature that permits
class (e.g., static) methods to be virtual and be overridden
in derived classes.

Related to that, given:

public class Foo
{
static void SomeMethod()
{
// How can I tell what derived class this
// static method was called through?
}
}

public class Bar : Foo
{
/...
}

Bar ABar = new Bar();
ABar.SomeMethod();

In the above, I want the method implementation
in Foo() to determine the class it was called through
(in this case, Bar).

Any suggestions are welcome
--
Tony M.
 
J

Jon Shemitz

Tony said:
Don't know if there are any Delphi/Object Pascal converts
here,

I see quite a few, actually.
I sorely miss the feature that permits
class (e.g., static) methods to be virtual and be overridden
in derived classes.

Why? Just curious.
public class Foo
{
static void SomeMethod()
{
}
}

public class Bar : Foo
{
}

Bar ABar = new Bar();
ABar.SomeMethod();

This won't compile ... you want Bar.SomeMethod() ....
In the above, I want the method implementation
in Foo() to determine the class it was called through
(in this case, Bar).

I'm drawing a blank. System.Reflection.MethodBase.GetCurrentMethod()
returns the current method, but when you call Foo.SomeMethod as
Bar.SomeMethod(), the MethodBase shows Foo for both the DeclaringType
and the ReflectedType.

Does it HAVE to be a static method?
 
O

Oliver Sturm

Tony said:
Don't know if there are any Delphi/Object Pascal converts
here, but being one, I sorely miss the feature that permits
class (e.g., static) methods to be virtual and be overridden
in derived classes.

This is not possible in C#. From the MSDN docs at
http://shrinkster.com/7ir: You cannot override a non-virtual or static
method. The overridden base method must be virtual, abstract, or override.
Related to that, given:

public class Foo
{
static void SomeMethod()
{
// How can I tell what derived class this
// static method was called through?
}
}

public class Bar : Foo
{
/...
}

Bar ABar = new Bar();
ABar.SomeMethod();

In the above, I want the method implementation
in Foo() to determine the class it was called through
(in this case, Bar).

This doesn't make any sense to me in this exact syntax, because you
can't even call the static method on the instance of Bar. You could call
Bar.SomeMethod(), which is really the same thing as calling
Foo.SomeMethod().

You can't find out in Foo that the static method was called via the
class Bar - in fact, the code that is generated for a call to
Bar.SomeMethod() is exactly the same as that generated for a call to
Foo.SomeMethod(). Try compiling a test program where you call the Foo
static method via the derived class and decompile the program in
Reflector. You'll see that the disassembled code shows a call to the
static method via the base class, not the derived class.

So, as you were asking for suggestions: tell us more about the purpose
of the class structure you are trying to create and we might be able to
suggest alternatives.



Oliver Sturm
 

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