P
Peter Morris
Just wondering what the reason is that an attribute cannot be generic?
Just wondering what the reason is that an attribute cannot be generic?
Just wondering what the reason is that an attribute cannot be generic?
Pavel said:See Jon's reply here:
http://stackoverflow.com/questions/294216/why-does-c-forbid-generic-attribute-types
to sum it up:
"Answer from Eric Lippert (paraphrased): no particular reason, except
to avoid complexity in both the language and compiler for a use case
which doesn't add much value."
Peter said:Generics is a feature which was added, with this in mind I expected to be
able to use it on System.Attribute too, but it seems it is a feature that
has not been implemented entirely.
I'd find generic attributes useful sometimes, just as I find generic
classes
useful, sometimes.
A lot of people find generic classes useful.
If enough people tell MS that generic attributes would
be useful, then I am sure that MS will eventually add it.
Peter Morris said:Only features that would benefit a lot users should be added.
As an example I shall show my ignorance of the validation block in MS P&P.
pubilc enum PersonStatus
{
InitialDataEntry,
Live
}
public class Person
{
[NotNullValidator]
public string Name { get; set; }
public PersonStatus Status { get; }
public DateTime DateOfBirth { get; set; }
public DateTime DateOfDeath { get; set; }
}
Not sure if the validation block will allow a complex validation such as
Status != PersonStatus.Live || DateOfBirth <= DateOfDeath
but with a generic attribute I could do something like this
[ExpressionValidator<Person>(p => p.Status != PersonStatus.Live ||
p.DateOfBirth <= p.DateOfDeath, "Some error message")]
pubilc class Person
{
}
Just the first example I could think of. I am sure there are many more.
--
Pete
====
http://mrpmorris.blogspot.com
http://www.AlterEgos.com - Who do you want to be?
Peter Morris said:Only features that would benefit a lot users should be added.
As an example I shall show my ignorance of the validation block in MS P&P.
pubilc enum PersonStatus
{
InitialDataEntry,
Live
}
public class Person
{
[NotNullValidator]
public string Name { get; set; }
public PersonStatus Status { get; }
public DateTime DateOfBirth { get; set; }
public DateTime DateOfDeath { get; set; }
}
Not sure if the validation block will allow a complex validation such as
Status != PersonStatus.Live || DateOfBirth <= DateOfDeath
but with a generic attribute I could do something like this
[ExpressionValidator<Person>(p => p.Status != PersonStatus.Live ||
p.DateOfBirth <= p.DateOfDeath, "Some error message")]
pubilc class Person
{
}
Just the first example I could think of. I am sure there are many more.
--
Pete
====
http://mrpmorris.blogspot.com
http://www.AlterEgos.com - Who do you want to be?