Click here to Skip to main content
15,889,595 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a custom attribute class SoftwareAttribute where i need to set some string and datetime value.
C#
[AttributeUsage(AttributeTargets.All)]
    class SoftwareAttribute : Attribute
    {
        string projectName;
        DateTime startDate, endDate;
        public SoftwareAttribute(string pname, DateTime start, DateTime end)
        {
            projectName = pname;
            startDate = start;
            endDate = end;
        }
        public virtual string ProjectName
        {
            get { return projectName; }
            set { projectName = value; }
        }
      
        public virtual DateTime StartDate
        {
            get { return startDate; }
            set { startDate = value; }

        }
        public virtual DateTime EndDate
        {
            get { return endDate; }
            set { endDate = value; }
        }
    }


Now Payroll class is as follow

C#
[Software("Payroll ,new DateTime(2013,10,15), new DateTime(2013,11,10))]
    class Payroll
    {
    }

As i am trying to add 'new DateTime(2013,10,15)'. This statement raises error:-

'An attribute argument must be a constant expression, typeof expression or array creation expression of an attribute parameter type'

How can i set datetime for startdate and enddate attribute from Payroll class.
And I don't want to use string for datetime.
Thank you.
Posted
Updated 15-Oct-13 17:41pm
v5

Unfortunately, as you've discovered, attributes won't allow you to supply as arguments values that must be allocated with new.

Which leaves you with having to make a compromise. Without knowing the larger context of the problem you're trying to solve with the attribute, what I would do in the situation is mae the start and end dates a string, not a DateTime.

Since you're capturing their values in the attribute's constructor, you can validate them in that constructor - and throw an exception if an invalid date string was supplied. Not the prettiest solution, but it scratches the itch.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 15-Oct-13 12:26pm    
Correct, 5ed.
The limitations of .NET attributes are really draconian.
—SA
Attribute parameters are restricted to constant values of the following types:

  • Simple types (bool, byte, char, short, int, long, float, and double)
  • string
  • System.Type
  • enums
  • object (The argument to an attribute parameter of type object must be a constant value of one of the above types.)
  • One-dimensional arrays of any of the above types


Replace your DateTime property with a string that is parsed at run time.

/ravi
 
Share this answer
 
Comments
[no name] 15-Oct-13 23:44pm    
Thank You all.
Both the answers are appreciable

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900