473,748 Members | 2,461 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Static property of type parameter - is this a CLR or C# limitation?

Here comes a generic class with a static property.
Let's say they are exotic singleton pets.
abstract class Pet {...}

abstract class SharedPet<T>: Pet
where T: SharedPet<T>, new()
{
private static T singleton = new T();
public T Singleton {get {return singleton;}}
}

class KingKong : SharedPet<KingK ong> {...}
class Gozilla : SharedPet<Gozil la> {...}
I use pets as a parameter to another generic class:
class PetLover<T>
where T : Pet, new()
{
public virtual T GetAPet
{ return new T(); } //Let it rain cats and dogs...
}

class SharedPetLover< T> : PetLover<T>
where T : SharedPet<T>, new()
{
public override T GetAPet //...but not gozillas
{ return T.Singleton; }
// Compile time error:
// 'T' is a 'type parameter', which is not
// valid in the given context'
}

Unforunately, it seems I can't access a static member
of T, though the constraint ensures it is there.

Is this a restriction of C#, or is it a CLR thing?
Is there a neat way of getting around it, or am I left
with the options of either rewriting the GetAPet
override using reflection, or doing copy-paste
overrides in KingKongLover, GozillaLover,
KermitLover, ReptarLover...?
(In case anyone wonders what the "real beasts"
look like: I'm trying to do a continuation passing
style implementation of a programming language
with a DOM like data model, and the nodes used
to implement continuations etc. must be as slim
as possible, yet compliant with the data model.)
May 14 '06 #1
4 5071
"Ole Nielsby" <ol*********@sn ailmail.dk> wrote:
abstract class SharedPet<T>: Pet
where T: SharedPet<T>, new()
{
private static T singleton = new T();
public T Singleton {get {return singleton;}}
This property is not static - but it doesn't affect your situation.

A more concise summary of the situation, I think, is this:

---8<---
class A<T>
{
public static T Value
{
get { return default(T); }
}
}

class B<T,U>
where T : A<U>
{
public U Value
{
get { return T.Value; }
}
}
--->8---
Unforunately, it seems I can't access a static member
of T, though the constraint ensures it is there.

Is this a restriction of C#, or is it a CLR thing?
When a generic class is being compiled to MSIL (CIL), every reference to
a method or field (properties are accessed via get and set methods) gets
turned into a metadata token, one of MethodRef, MethodDef, FieldDef,
FieldRef (*Ref come from other assemblies, *Def are defined in the same
assembly).

Instance members are known from constraints because there is an actual
metadata token corresponding to each the base class constraint's members
(if any) and to each method defined in each interface constraint.

To the best of my knowledge, there's no way in IL to represent an as-yet
unbound methodref/methoddef in the way that you require. In the raw
assembly, the tokens are referred to via integer keys into a dictionary
associated with the assembly - thus the representation doesn't encode
what would be required in your case, somehow passing the generic type
argument to the target generic metadata token.

So, I would say it is a current architectural limitation of the CLR.
Is there a neat way of getting around it, or am I left
with the options of either rewriting the GetAPet
override using reflection, or doing copy-paste
overrides in KingKongLover, GozillaLover,
KermitLover, ReptarLover...?
I would suggest using a factory object approach. If you're familiar with
languages that support metaclasses (Delphi, Python, Smalltalk, etc.),
implement something similar. Basically, turn your static members into
instance members on a separate singleton class. You might need to pass
an extra parameter to constructors in your generic classes to support
this.

Alternatively, if you can take the performance hit, you might attribute
your classes with the relevant data you need, and pull that information
out via reflection.
(In case anyone wonders what the "real beasts"
look like: I'm trying to do a continuation passing
style implementation of a programming language


Good luck implementing it on .NET - the best I could come up with when
implementing something similar was a registry of methods, and returning
the next (method, arguments) to call to a dispatcher loop.

-- Barry
May 14 '06 #2

Barry Kelly <ba***********@ gmail.com> wrote:
[...some insights in generics compilation...]

So, I would say it is a current architectural limitation of the CLR.


Thanks for a detailed explanation.

I'll go for a combination of reflection and static fields. Using
reflection in the class initializer will be faster than cluttering
the constructors with factory parameters, I think.

My SharedPetLover< T> gets a static field that is filled in by
using reflection to get the SharedPet<T> singleton. Or,
expanding on your version:

class A<T>
{
public static T Value
{
get { return default(T); }
}
}

class B<T,U>
where T : A<U>
{
public U Value
{
get { return value; }
}
private static U value =
(U)typeof(U).In vokeMember(
"Value", flags, null, null, null);
const flags =
BindingFlags.Ge tProperty
| BindingFlags.Pu blic
| BindingFlags.St atic
| BindingFlags.Fl attenHierarchy;
}
This may complicate the initialization process a bit,
but once it's done I get fast object creation. (I'm
still slightly puzzled that it can't be done without
reflection or factories, but I'll take your words for
that.)

Regards/Ole N.
May 14 '06 #3
"Ole Nielsby" <ol*********@sn ailmail.dk> wrote:

Barry Kelly <ba***********@ gmail.com> wrote:
[...some insights in generics compilation...]

So, I would say it is a current architectural limitation of the CLR.


Thanks for a detailed explanation.


Belay that order! Your post got me thinking, and I realised that the
same "problem" of passing the generic argument would occur for instance
methods. So I investigated further with ILASM. I patched up the IL that
referred to an instance property to refer to a static property instead.
It compiled correctly with ilasm, and ran correctly.

I then ran it through Reflector to see what it thought of the IL. Then I
saw the error of my ways...

You can indeed access a static property of a type argument, via the more
prosaic route of using the same type as specified in the constraint:

---8<---
class A<T>
{
public static T StaticValue
{
get { return default(T); }
}
}

class B<T,U>
where T : A<U>
{
public B()
{
}

public U Value
{
get { return A<U>.StaticValu e; }
}
}
--->8---

Don't forget that the storage for a static field is the shared for all
its descendants - there isn't a unique copy of statics per descendant
class, so the limitation on directly accessing T isn't actually a
limitation at all.

Sorry for overcomplicatin g matters...

HTH,

-- Barry
May 14 '06 #4

Barry Kelly <ba***********@ gmail.com> wrote:
Belay that order! Your post got me thinking, and I [...]
saw the error of my ways...


Out goes using System.Reflecti on; and my code suddenly
looks more readable.

At last I can go to sleep without feeling too stupid!
May 14 '06 #5

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

Similar topics

12
24810
by: Sergey Klementiev | last post by:
Why it's impossible to have a static indexer in C#?
3
5371
by: John Wood | last post by:
The GetProperty ./ GetMethod etc. methods all require an instance as a first parameter. What if the property is static? I can't pass in a class, obviously... John
11
3842
by: Kevin Prichard | last post by:
Hi all, I've recently been following the object-oriented techiques discussed here and have been testing them for use in a web application. There is problem that I'd like to discuss with you experts. I would like to produce Javascript classes that can be "subclassed" with certain behaviors defined at subclass time. There are plenty of ways to do this through prototyping and other techniques, but these behaviors need to be static and...
8
8931
by: Per Bull Holmen | last post by:
Hey Im new to c++, so bear with me. I'm used to other OO languages, where it is possible to have class-level initialization functions, that initialize the CLASS rather than an instance of it. Like, for instance the Objective-C method: +(void)initialize Which has the following characteristics: It is guaranteed to be run
0
6990
by: Bryce Fischer | last post by:
I've got a simple (I think) asp.net application. I've created a DataSet in App_Code/ItemDataSet.xsd. Tested connection, seemed to work fine. In my ASPX file, I first dropped an ObjectDataSource onto the form, and pointed it to the dataset created above. I then dropped a GridView on the form, and selected the ObjectDataSource I created above. In the Design view it seems to be at least loading the
2
2412
by: cmonthenet | last post by:
Hello, I searched for an answer to my question and found similar posts, but none that quite addressed the issue I am trying to resolve. Essentially, it seems like I need something like a virtual static function (which I know is illegal), but, is there a way to provide something similar? The class that is the target of my inquiry is a template class that interfaces to one of several derived classes through a pointer to a base class. The...
2
2482
by: Ranganath | last post by:
Hi, Why is there a restriction that only integral types can be made static constant members of a class? For e.g., class B { private: static const double K = 10; };
3
1726
by: Filip Zawada | last post by:
Hi, I've encountered a rather interesting language problem. Suppose I've got following types: //class providing usefull static methods public class GoodBase<T> { public static void Met(){}; }
0
2987
by: SimonDotException | last post by:
I've written an abstract base type which uses generics to provide XML serialization and deserialization methods for use by its derived types, but I'm seemingly unable to write it in a way which completely satisfies FxCop code analysis. Here is the simplest form of the base class which reproduces the problem: public abstract class XmlSerializableItem<T> { public string ToXml() { XmlSerializer xs = new XmlSerializer( this.GetType() );...
0
8995
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8832
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
9331
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9253
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8250
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 project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6798
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4608
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3316
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2791
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.