Background
I'm trying to implement a custom attribute that can be applied to a .NET assembly to indicate an expiry date (the date after which the developers do not support the use of a pre-release version that has been distributed for testing). It has to be written in .NET Standard (2.0).
I know that I can't pass a DateTime
in as a parameter so I'm passing in a string that conforms to ISO8601 (YYYY-MM-DD) and then using DateTime.Parse()
to convert to a DateTime
.
The attribute I've got so far is below:
[AttributeUsage(AttributeTargets.Assembly, Inherited = false, AllowMultiple = false)]
public sealed class UnstableReleaseExpiryAttribute : Attribute
{
public UnstableReleaseExpiryAttribute(string expiryDate)
{
ExpiryDate = expiryDate;
}
public DateTime Expiry
{
get
{
if (ExpiryDate != null && DateTime.TryParse(ExpiryDate, out DateTime expiry))
return expiry;
return DateTime.MaxValue;
}
}
public string ExpiryDate { get; }
}
This is how I intend to use it:
[assembly: UnstableReleaseExpiry("2018-05-24")]
Question
Is there a way to validate the string input using, say, a regex expression, and stop it from being compiled if the date isn't actually parsable? I looked around and thought that inheriting from ValidationAttribute
would be the way to do it, but it doesn't appear to be available in .NET Standard 2.0. Are there any other ways of doing this?