On 27 Jul 2005 10:07:58 -0700, "Andrew" <ol*****@gmail.com> wrote:
What I'm trying to do is to make some sort of "static virtual" method
this way...Are there some other ways to achieve it?
Wait for Smalltalk for .NET, I suppose... as Oliver said, C# does not
have virtual statics. That's by design and I don't think it's going to
change.
If this is really important you could manually write out the various
code paths depending on the instance type of T, though:
class B<T> where T : A
{
public static void SB()
{
if (typeof(T) == typeof(A))
A.SA();
else if (typeof(T) == typeof(C))
C.SA();
}
}
Naturally, you'll lose the main advantages of polymorphism this way --
the automatic code path determination and the ability to define new
functionality in derived types.
Another option would be to use reflection to invoke the desired
member:
using System.Reflection;
class B<T> where T : A
{
public static void SB()
{
typeof(T).InvokeMember("SA",
BindingFlags.Public | BindingFlags.Static,
null, null, null);
}
}
That's nicely polymorphic, but it has the disadvantage that the method
is searched by its name whenever SB() is called, so it's quite slow.
You could make a hashtable that maps T types to buffered MethodInfo
objects for each type, though...
--
http://www.kynosarges.de