locked
Attributes and new features in C# 2.0 and 3.0

    Question

  • Hi,

    Have you try to use attributes with the new features of C# 3.0? For example this code:

        [MyAttribute( x => x + 1 ) ]
        class Program
        {
            static void Main(string[] args)
            {
            }
        }

        public class MyAttribute : Attribute
        {
            public MyAttribute( Func<int,int> d )
            {
            }
        }
    Makes VS.NET 2005 throw an unmanaged exception, and the command line compiler does not generate any files?

    So the question is divided in two parts: Is there any plan to support lambdas in attributes? 

    And the second question is related to C# 2.0, a template class can't be used as an attribute (public class MyA<T> : Attribute {}). (error CS0404: '<' unexpected : attributes cannot be generic). There are plans to support this in C# 3.0?

    Regards,

    Thursday, February 16, 2006 4:59 PM

Answers

  • Attributes are encoded in the module differently than code blocks.  The constructor arguments and property values can only hold constants of primative numeric, string or Type values.
    Tuesday, February 21, 2006 6:15 AM

All replies

  • My sympathies: I've wanted these as well...
    Saturday, February 18, 2006 3:07 AM
  • Thank Keith, I was reading the post and I've not detailed why this feature could be great. Some time ago I've created an invariant validator for .NET classes, but the problem was that expressions could not be declated in attributes so I have to describe them as strings:

         [InvariantBool( "Total == SubTotal + Taxes" )]

         public int Total{ //... }

    When C# 2.0 appears I was very interested in the generics because I can implement some simple expressions as attributes:

         [LowerThan<int>( 22 )]

         public int Total{ //... }

    And it was frustrating to see how it was leaved ot uf the scope in the spec. When I starter reading about lambdas I figure out that I could get rid of the string in the expressions, but It was frustrating again see that it was not implemented.

    My question is: Is this out of the scope explicitly?

    Regards,

    Monday, February 20, 2006 2:17 AM
  • Generic attributes would (I believe) be something that the CLR team would be most likely to address.  In general, validation metadata is something that would probably be owned by a group like the BCL, but stay tuned.

     

    Monday, February 20, 2006 6:37 AM
  • Attributes are encoded in the module differently than code blocks.  The constructor arguments and property values can only hold constants of primative numeric, string or Type values.
    Tuesday, February 21, 2006 6:15 AM
  • ... which, among other things, prevents constructs such as [Range<DateTime>(Start = new DateTime(2006, 1, 1), End = new DateTime(2006, 12, 31))]:

    RangeAttribute<T> : Attribute
    {
        public T Start { get; set; }

        public T End { get; set; }

        public bool IsInRange(T value) { return Start <= value && value <= End; }
    }

     

    Tuesday, February 21, 2006 6:37 AM