471,317 Members | 1,378 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

C# Generics, where keyword, and attributes.

Hi,

I'm playing around with extended List<T> such that I have a
generic list collection that is cloneable (fully, not shallow) and
serializable.

So far the impl looks like this.

using System;
using System.Collections.Generic;
using System.Runtime.Serialization;
using System.Text;

[Serializable]
public class CloneableList<T> : List<T>, ICloneable
where T : ICloneable, ISerializable
{
public CloneableList() { }

public CloneableList(int size) : base(size) { }

public object Clone()
{
CloneableList<T> clone = new CloneableList<T>(this.Count);

foreach (T t in this)
{
clone.Add((T)t.Clone());
}

return clone;
}
}

However, I note that the constraint above where T : ISerializable, is
too
strict, as it rules out some of the normal classes that are marked with
the [Serializable] attribute, but do not implement ISerializable (like
System.String for instance)

Is there anyway to use the where keyword to specify that T must
have the [Serializable] attribute? Or is the design of attributes in
C# such that any attributes on T cannot be known at the time that
CloneableList<T> is compiled into, say, CloneableList<String>?

Thanks,

Eric

Feb 7 '06 #1
2 19157
Ricebot wrote:
I'm playing around with extended List<T> such that I have a
generic list collection that is cloneable (fully, not shallow) and
serializable.
<snip>
Is there anyway to use the where keyword to specify that T must
have the [Serializable] attribute? Or is the design of attributes in
C# such that any attributes on T cannot be known at the time that
CloneableList<T> is compiled into, say, CloneableList<String>?


I don't know that the latter is true, but there's no way of specifying
what you want, unfortunately.

Jon

Feb 7 '06 #2
Ok, thanks for the feedback!

Jon Skeet [C# MVP] wrote:
Ricebot wrote:
I'm playing around with extended List<T> such that I have a
generic list collection that is cloneable (fully, not shallow) and
serializable.


<snip>
Is there anyway to use the where keyword to specify that T must
have the [Serializable] attribute? Or is the design of attributes in
C# such that any attributes on T cannot be known at the time that
CloneableList<T> is compiled into, say, CloneableList<String>?


I don't know that the latter is true, but there's no way of specifying
what you want, unfortunately.

Jon


Feb 7 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

66 posts views Thread by Darren Dale | last post: by
4 posts views Thread by Brian | last post: by
3 posts views Thread by Pierre Y. | last post: by
3 posts views Thread by Marshal | last post: by
5 posts views Thread by Mark | last post: by
4 posts views Thread by Bruno Neves Pires Silva | last post: by
reply views Thread by rosydwin | last post: by

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.