473,414 Members | 1,746 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

Generics: static member invocation

Hello All!

I'm trying to compile the following code in VS2005 Beta
class A
{
public static void SA() { }
}

class B<T> where T : A
{
public static void SB()
{
T.SA();
}
}

And I'm getting the following error
Error 7 'T' is a 'type parameter', which is not valid in the given
context

My question is: Is it a bug or feature?

Thanks in advance

Nov 17 '05 #1
5 1741
Andrew wrote:
I'm trying to compile the following code in VS2005 Beta
class A
{
public static void SA() { }
}

class B<T> where T : A
{
public static void SB()
{
T.SA();
}
}

And I'm getting the following error
Error 7 'T' is a 'type parameter', which is not valid in the given
context

My question is: Is it a bug or feature?


I'm quite sure it's as intended, with this reasoning: You can only call
a static method of a type you know beforehand. In your sample, you can
only call SA() on T because you know T is an A. So you could achieve the
exact same result by calling A.SA(); instead of T.SA();.

Although it looks logical at first glance, the T.SA() syntax actually
introduces an unnecessary level of complexity to the expression with no
additional benefit, I think that's why the compiler doesn't allow it.

Oliver Sturm
--
omnibus ex nihilo ducendis sufficit unum
Spaces inserted to prevent google email destruction:
MSN oliver @ sturmnet.org Jabber sturm @ amessage.de
ICQ 27142619 http://www.sturmnet.org/blog
Nov 17 '05 #2
I guess if I replace T.SA() with A.SA() the result will not always be
the same
Suppose I also have
class C : A
{
public static new void SA() { }
}
In this case, I want B<C>.SB() to call C.SA(), not A.SA()

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?


Oliver Sturm писал(а):
Andrew wrote:
I'm trying to compile the following code in VS2005 Beta
class A
{
public static void SA() { }
}

class B<T> where T : A
{
public static void SB()
{
T.SA();
}
}

And I'm getting the following error
Error 7 'T' is a 'type parameter', which is not valid in the given
context

My question is: Is it a bug or feature?


I'm quite sure it's as intended, with this reasoning: You can only call
a static method of a type you know beforehand. In your sample, you can
only call SA() on T because you know T is an A. So you could achieve the
exact same result by calling A.SA(); instead of T.SA();.

Although it looks logical at first glance, the T.SA() syntax actually
introduces an unnecessary level of complexity to the expression with no
additional benefit, I think that's why the compiler doesn't allow it.

Oliver Sturm
--
omnibus ex nihilo ducendis sufficit unum
Spaces inserted to prevent google email destruction:
MSN oliver @ sturmnet.org Jabber sturm @ amessage.de
ICQ 27142619 http://www.sturmnet.org/blog


Nov 17 '05 #3
Andrew wrote:
I guess if I replace T.SA() with A.SA() the result will not always be
the same
Suppose I also have
class C : A
{
public static new void SA() { }
}
In this case, I want B<C>.SB() to call C.SA(), not A.SA()

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?


I guess that's just the point: there are no virtual static methods in
C#, and you can't trick the system this way.

In your sample, the problem is that either the method that's being
called is really the same as if you were calling A.SA(), or you are
assuming something about the derived class C that's just not true,
because there are no virtual static methods.

I remember seeing a video (a whiteboard talk or something) where Anders
gave some of the reasons for not doing virtual static methods in C#, but
I don't remember where I saw it. Maybe someone else knows a source for that?

Oliver Sturm
--
omnibus ex nihilo ducendis sufficit unum
Spaces inserted to prevent google email destruction:
MSN oliver @ sturmnet.org Jabber sturm @ amessage.de
ICQ 27142619 http://www.sturmnet.org/blog
Nov 17 '05 #4
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
Nov 17 '05 #5
Christoph Nahr wrote:
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...


And you might have to be careful to make sure that the static method is
really found. There's this thing about "flattening the hierarchy" (one
of the BindingFlags) that's sometimes needed when calling static members
by Reflection, depending on whether the type you use actually has its
own implementation of the static method or it's using the base class
one. Just a heads up.

Oliver Sturm
--
omnibus ex nihilo ducendis sufficit unum
Spaces inserted to prevent google email destruction:
MSN oliver @ sturmnet.org Jabber sturm @ amessage.de
ICQ 27142619 http://www.sturmnet.org/blog
Nov 17 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: john smith | last post by:
I'm wondering if it's possible to declare a pure virtual member function? Ie is: class A{ public: virtual static void f() const = 0; }; legal? I'm getting compile errors for code that used...
30
by: Joost Ronkes Agerbeek | last post by:
Why is it allowed in C++ to call a static member function of an object through an instance of that object? Is it just convenience? tia, Joost Ronkes Agerbeek
12
by: cppaddict | last post by:
Hi, I know that it is illegal in C++ to have a static pure virtual method, but it seems something like this would be useful when the following 2 conditions hold: 1. You know that every one...
17
by: Andreas Huber | last post by:
What follows is a discussion of my experience with .NET generics & the ..NET framework (as implemented in the Visual Studio 2005 Beta 1), which leads to questions as to why certain things are the...
3
by: Kirk Marple | last post by:
Just want to see if this is 'by design' or a bug... I have a common List<T> defined in a base class, and the base class has a static property to expose this list. I wanted the derived class to...
6
by: Bill Rubin | last post by:
The following code snippet shows that VC++ 7.1 correctly compiles a static member function invocation from an Unrelated class, since this static member function is public. I expected to compile the...
4
by: taub.adam | last post by:
Hi, Can generics be used to call a static method of a class? The below example gives an error in my generictest class. Anybody know why? class foo { static public void Test() {...
1
by: Vladimir Shiryaev | last post by:
Hello! Exception handling in generics seems to be a bit inconsistent to me. Imagine, I have "MyOwnException" class derived from "ApplicationException". I also have two classes...
7
by: SpotNet | last post by:
Hello NewsGroup, Reading up on Generics in the .NET Framework 2.0 using C# 2005 (SP1), I have a question on the application of Generics. Knowingly, Generic classes are contained in the...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing,...

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.