473,385 Members | 1,333 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,385 software developers and data experts.

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
Nov 16 '07 #1
5 2749
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
Nov 16 '07 #2
On Nov 16, 11:48 am, "Marc Gravell" <marc.grav...@gmail.comwrote:

<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
Nov 16 '07 #3
"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.
Nov 16 '07 #4
[...] 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
Nov 16 '07 #5
Matteo Casati <Ma**********@discussions.microsoft.comwrote:
[...] 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 - <sk***@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
Nov 16 '07 #6

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

Similar topics

2
by: Wiktor Zychla | last post by:
I've read several documents about upcoming C# generics and I still cannot understand one thing. Would it be valid to write a code like this: class SomeClass { public void AMethod<T>(T a, T...
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...
10
by: Ruediger Klaehn | last post by:
Sorry about the harsh language, but I have to vent my anger to somebody who actually understands what I am talking about. Complaining to my girlfriend is not going to produce any meaningful results...
4
by: Tom Jastrzebski | last post by:
Hello everybody, Here is the problem I came across experimenting with Generics. I would like to write a class or a struct adding integer to any other, initially undefined *numeric type*. So, my...
17
by: atgraham | last post by:
Here is the "lead C# architect" attempting to impugn C++ templates (bottom of the page). http://www.artima.com/intv/generics2.html (bottom of the page) (Full article begins at...
11
by: herpers | last post by:
Hello, I probably don't see the obvious, but maybe you can help me out of this mess. The following is my problem: I created two classes NormDistribution and DiscDistribution. Both classes...
9
by: sloan | last post by:
I'm not the sharpest knife in the drawer, but not a dummy either. I'm looking for a good book which goes over Generics in great detail. and to have as a reference book on my shelf. Personal...
19
by: Barry Mossman | last post by:
Hi, if I have a generic class such as: public class MyGroup<T> : Collection<MyChildClass> and from one of it's methods I want to pass a reference to myself to the following method in another...
6
by: =?Utf-8?B?UXVhbiBOZ3V5ZW4=?= | last post by:
I am trying to create a generics class with multiple constrains, as follows: public class KeyHandler<Twhere T : TextBoxBase, ComboBox When I try that, the compiler would complain: The class...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.