Why can't attributes be generic?

  • Thread starter Thread starter Peter Morris
  • Start date Start date
Just wondering what the reason is that an attribute cannot be generic?

Hi,

Maybe because of the way that generics are resolved (at runtime) and
attributes should be resolved at compile time.

I cannot think of another reason
 
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."

Which is a very fair point of view.

There must be a gazillion features that could be added to
a language that would at least benefit one user. But implementing
them all would make the language way too bloated and it would
go the way of PL/I and T-Rex.

Only features that would benefit a lot users should be added.

Arne
 
Only features that would benefit a lot users should be added.

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.


Pete
====
http://mrpmorris.blogspot.com
http://www.AlterEgos.com - Who do you want to be?
 
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.

Arne
 
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.
 
I really do not see what this gives you. When are you going to actually run
validation on the object? Are you not better deriving all Model Objects from
a base class that defines a validate method that is overridable??? This also
ensures a continuity between your validation rather than redefining it
everytime you wish to validate.

I fail also from you example to see how you make this

Status != PersonStatus.Live || DateOfBirth <= DateOfDeath

Surly PersonStatus is get and one presumes driven from DateOfBirth <=
DateOfDeath return dead else if DateOfDeath == null return Live. Don't
forget when setting the date of death to check it is after birth date, and
this is when you start thinking is this specific BLL that may change and
hence should reside in some BLL especially when you factor in more complex
issues too horrible to mention...




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?
 
I really do not see what this gives you. When are you going to actually run
validation on the object? Are you not better deriving all Model Objects from
a base class that defines a validate method that is overridable??? This also
ensures a continuity between your validation rather than redefining it
everytime you wish to validate.

I fail also from you example to see how you make this

Status != PersonStatus.Live || DateOfBirth <= DateOfDeath

Surly PersonStatus is get and one presumes driven from DateOfBirth <=
DateOfDeath return dead else if DateOfDeath == null return Live. Don't
forget when setting the date of death to check it is after birth date, and
this is when you start thinking is this specific BLL that may change and
hence should reside in some BLL especially when you factor in more complex
issues too horrible to mention...




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?
 
Back
Top