DateTime Literal

  • Thread starter Thread starter Robert Bouillon
  • Start date Start date
R

Robert Bouillon

I believe I may be missing something obvious, as I've found nothing in my
online searches regarding DateTime literals in C#.

I have a custom attribute that takes in a DateTime as a argument. Just to
recap: Since attributes are compiled to Meta-Data, the value must be a
literal, so I can't use DateTime.Parse.

Is there a way to specify a DateTime literal without passing in an
alternative type (Such as a String or UTC value?).

The string has globalization issues and the UTC is difficult to obtain and
nearly impossible to decipher with the naked eye. A C# DateTime literal
would be ideal.

Thanks
--ROBERT
 
Robert Bouillon said:
I believe I may be missing something obvious, as I've found nothing in my
online searches regarding DateTime literals in C#.

There's no such thing.
I have a custom attribute that takes in a DateTime as a argument.

Well that's a problem to start with. From the C# language spec, section
24.1.3 (ECMA numbering):

<quote>
The types of positional and named parameters for an attribute class are
limited to the attribute parameter types, which are:

One of the following types: bool, byte, char, double, float, int,
long, short, string.
The type object.
The type System.Type.
An enum type, provided it has public accessibility and the types in
which it is nested (if any) also have public accessibility.
Single-dimensional arrays of the above types.
</quote>
 
Which means...

Declare your date field as a string and parse it inside the attribute class.
 
Thanks.

Actually I went with two args:

STRING: localDate
STRING: cultureName

The date is then parsed using the culture specified. Default culture is
"en-US".

--ROBERT

Jonathan Allen said:
Which means...

Declare your date field as a string and parse it inside the attribute class.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top