C# Question with Generics
-
I posted a question earlier re. genarics for .net 2.0 and seems that what I'm trying to do is unfeasible. So here's my last (I think it's last) question on generics: Is it possible to reference "current class" instead of hard-coding the class name in the code? E.g.
abstract class BaseClass
{
static void GenericStaticMethod < T > (args)
{
...
}
void Foo()
{
GenericStaticMethod <CurrentClass>.(args);
}
}class C1 : BaseClass
{
...
}
class C2 : BaseClass
{
...
}when calling
C1.Foo()
, it should in turn callGenericStaticMethod < C1 > (...)
or callingC2.Foo(),
it should in turn callGenericStaticMethod < C2 > (...)
? - Malhar -
I posted a question earlier re. genarics for .net 2.0 and seems that what I'm trying to do is unfeasible. So here's my last (I think it's last) question on generics: Is it possible to reference "current class" instead of hard-coding the class name in the code? E.g.
abstract class BaseClass
{
static void GenericStaticMethod < T > (args)
{
...
}
void Foo()
{
GenericStaticMethod <CurrentClass>.(args);
}
}class C1 : BaseClass
{
...
}
class C2 : BaseClass
{
...
}when calling
C1.Foo()
, it should in turn callGenericStaticMethod < C1 > (...)
or callingC2.Foo(),
it should in turn callGenericStaticMethod < C2 > (...)
? - MalharI don't think so. The best way to accomplish what you're trying to do is to make Foo() virtual, then override Foo in the base classes to do call GenericStaticMethod using the current class type.
Tech, life, family, faith: Give me a visit. I'm currently blogging about: The Lord's Prayer in Aramaic song (audio) The apostle Paul, modernly speaking: Epistles of Paul Judah Himango
-
I don't think so. The best way to accomplish what you're trying to do is to make Foo() virtual, then override Foo in the base classes to do call GenericStaticMethod using the current class type.
Tech, life, family, faith: Give me a visit. I'm currently blogging about: The Lord's Prayer in Aramaic song (audio) The apostle Paul, modernly speaking: Epistles of Paul Judah Himango
-
I posted a question earlier re. genarics for .net 2.0 and seems that what I'm trying to do is unfeasible. So here's my last (I think it's last) question on generics: Is it possible to reference "current class" instead of hard-coding the class name in the code? E.g.
abstract class BaseClass
{
static void GenericStaticMethod < T > (args)
{
...
}
void Foo()
{
GenericStaticMethod <CurrentClass>.(args);
}
}class C1 : BaseClass
{
...
}
class C2 : BaseClass
{
...
}when calling
C1.Foo()
, it should in turn callGenericStaticMethod < C1 > (...)
or callingC2.Foo(),
it should in turn callGenericStaticMethod < C2 > (...)
? - MalharGenericStaticMethod .(args);
Does not work? /M -
GenericStaticMethod .(args);
Does not work? /M