TechTalkz.com Logo Ask the Experts!

Go Back   TechTalkz.com Technology & Computer Troubleshooting Forums > Tech Support Archives > Programing Languages > C#(C Sharp)

Generics: how to have a constraint on an attribute?

C#(C Sharp)

 
 
Thread Tools Display Modes
Unread 16-11-2007, 12:02 PM   #1
Matteo Casati
Guest
 
Posts: n/a
Generics: how to have a constraint on an attribute?

Hi,
I would like to set a constraint (where keyword) on a type in a generic
class, based on the [Serializable] attribute to accept as "T" type each class
marked as serializable.
Is it possible? If yes, how to do? Else... why not??? :-)
Thanks


 
Unread 16-11-2007, 12:02 PM   #2
Marc Gravell
Guest
 
Posts: n/a
Re: Generics: how to have a constraint on an attribute?

> Is it possible?
Not at compile time via a constraint, no.

At runtime, you could check via reflection, but you don't get many
benefits doing this.

> why not???

Remember that constraints are compile-time; how many permutations /
combinations should it support? what are the common use-cases? what
level of complexity? Personally I find that the existing limits do
what I need.
A more common question is "can I tell it to demand a constructor
matching (int, string)" etc, but again this isn't supported - just
parameterless.

Marc


 
Unread 16-11-2007, 01:03 PM   #3
Jon Skeet [C# MVP]
Guest
 
Posts: n/a
Re: Generics: how to have a constraint on an attribute?

On Nov 16, 11:48 am, "Marc Gravell" <marc.grav...***********> wrote:

<snip>

> A more common question is "can I tell it to demand a constructor
> matching (int, string)" etc, but again this isn't supported - just
> parameterless.


The two I'd like supported are "T derives from System.Enum" and "T
derives from System.Delegate" - but both of these are specifically
*excluded* from the normal derivation constraints, which is a bit of a
pain

Jon
 
Unread 16-11-2007, 01:03 PM   #4
Matteo Casati
Guest
 
Posts: n/a
Re: Generics: how to have a constraint on an attribute?

"Marc Gravell" wrote:


> Not at compile time via a constraint, no.


Ok :-(

> At runtime, you could check via reflection, but you don't get many
> benefits doing this.


Yeah, but this is not a solution: I will have compile time errors also
without the check on IsSerializable :-)

> Remember that constraints are compile-time; how many permutations /
> combinations should it support? what are the common use-cases? what
> level of complexity? Personally I find that the existing limits do
> what I need.


I agree!

Thank your for your reply.
Have a nice day.
 
Unread 16-11-2007, 01:03 PM   #5
Matteo Casati
Guest
 
Posts: n/a
Re: Generics: how to have a constraint on an attribute?

> [...] both of these are specifically
> *excluded* from the normal derivation constraints, which is a bit of a
> pain


Current supported constraints are too poor to be useful :-(
We can only hope to have an extension of "where" keywords in one of the the
next releases.

Thanks for your answer.
Bye
 
Unread 16-11-2007, 09:04 PM   #6
Jon Skeet [C# MVP]
Guest
 
Posts: n/a
Re: Generics: how to have a constraint on an attribute?

Matteo Casati <MatteoCasati@discussions.microsoft.com> wrote:
> > [...] both of these are specifically
> > *excluded* from the normal derivation constraints, which is a bit of a
> > pain

>
> Current supported constraints are too poor to be useful :-(


I wouldn't say that. They're not as useful as they *might* be, but
they're still useful.

> We can only hope to have an extension of "where" keywords in one of the the
> next releases.


Unfortunately I suspect that would require CLR support, which is
obviously more significant than just library or language support

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
 
 

Thread Tools
Display Modes



< Home - Windows Help - MS Office Help - Hardware Support >


New To Site? Need Help?

All times are GMT. The time now is 02:26 PM.


vBulletin, Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO
Copyright © 2005-2013, TechTalkz.com. All Rights Reserved - Privacy Policy
Valid XHTML 1.0 Transitional